Error Handling

Exception handling, reporting, and rendering

Laravel centralizes error handling in a single class. Exceptions flow through report and render methods.

The exception handler has two key methods: report() logs the exception, render() converts it to a response.

Exception flow
throwException thrown
handlerHandler catches
reportLog/notify
renderBuild response
responseSend to client
Common error pages
404Not Found
500Server Error
403Forbidden
419Page Expired
PHP
// app/Exceptions/Handler.php
class Handler extends ExceptionHandler {
    public function register(): void {
        // Report: log or send to external service
        $this->reportable(function (Throwable $e) {
            // Sentry::captureException($e);
        });

        // Render: convert exception to HTTP response
        $this->renderable(function (NotFoundHttpException $e) {
            return response()->json([
                'error' => 'Resource not found',
            ], 404);
        });
    }
}