Development
One of the advantages to working with a robust server-side framework is the built-in exception handling you get for free. For example, Laravel ships with a beautiful error reporting tool which displays a nicely formatted stack trace in local development. The challenge is, if you’re making an XHR request (which Inertia does) and you hit a server-side error, you’re typically left digging through the network tab in your browser’s devtools to diagnose the problem. Inertia solves this issue by showing all non-Inertia responses in a modal. This means you get the same beautiful error-reporting you’re accustomed to, even though you’ve made that request over XHR.Production
In production you will want to return a proper Inertia error response instead of relying on the modal-driven error reporting that is present during development. To accomplish this, you may use theInertia::handleExceptionsUsing() method in your application’s service provider.
withSharedData() explicitly resolves the Inertia middleware and includes your shared props in the error page.
The ExceptionResponse instance provides the exception, request, and response as public readonly properties, along with the following methods:
render($component, $props)- Render an Inertia page component with the given propswithSharedData()- Include shared data from the Inertia middlewareusingMiddleware($class)- Specify which Inertia middleware to use for shared data and root view resolutionrootView($view)- Set a custom root view for the error responsestatusCode()- Get the HTTP status code of the original response
withSharedData() typically works without specifying a middleware class. Returning null from the callback falls through to Laravel’s default exception rendering.
Error Page Example
You’ll need to create the error page components referenced above. Here’s an example you may use as a starting point.Manual Exception Handling
Under the hood,handleExceptionsUsing() registers a $exceptions->respond() callback in your application’s bootstrap/app.php file. You may register this callback manually if you prefer.