Error Handling
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.
For applications using a Content Security Policy that restricts inline styles, you may pass a nonce to createInertiaApp() to permit the inline styles the error modal injects. See Content Security Policy for details.
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. Use Inertia::handleExceptionsUsing() in your application's service provider.Override error_page() on your HandleInertiaRequests middleware.
1use Inertia\Inertia;2use Inertia\ExceptionResponse;34public function boot(): void5{6 Inertia::handleExceptionsUsing(function (ExceptionResponse $response) {7 if (in_array($response->statusCode(), [403, 404, 500, 503])) {8 return $response->render('ErrorPage', [9 'status' => $response->statusCode(),10 ])->withSharedData();11 }12 });13}1use Inertia\Inertia;2use Inertia\ExceptionResponse;34public function boot(): void5{6 Inertia::handleExceptionsUsing(function (ExceptionResponse $response) {7 if (in_array($response->statusCode(), [403, 404, 500, 503])) {8 return $response->render('ErrorPage', [9 'status' => $response->statusCode(),10 ])->withSharedData();11 }12 });13}
1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn error_page(&self) -> Option<&str> {5 Some("ErrorPage")6 }7}1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn error_page(&self) -> Option<&str> {5 Some("ErrorPage")6 }7}
Since exceptions like 404s occur outside of the Inertia middleware (the request never reaches your routes), the error response won't have access to shared data or the root view by default. Calling 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
The Inertia middleware is auto-resolved from the matched route or the kernel's middleware groups, so withSharedData() typically works without specifying a middleware class. Returning null from the callback falls through to Laravel's default exception rendering.
The error component receives an error prop holding status and message, along with your shared props (treat shared props as optional, since any shared prop that fails to resolve is omitted from the error page). Because unmatched URLs don't run route middleware, add route.fallback(async || laravel::abort(404)) at the end of src/routes/web.rs so 404 pages render through your Inertia error component as well. Requests that expect JSON still receive JSON, and server errors in debug mode (APP_DEBUG=true) still show the debug page.
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.
1<script setup>2import { computed } from "vue";34const props = defineProps({ status: Number });56const title = computed(() => {7 return {8 503: "503: Service Unavailable",9 500: "500: Server Error",10 404: "404: Page Not Found",11 403: "403: Forbidden",12 }[props.status];13});1415const description = computed(() => {16 return {17 503: "Sorry, we are doing some maintenance. Please check back soon.",18 500: "Whoops, something went wrong on our servers.",19 404: "Sorry, the page you are looking for could not be found.",20 403: "Sorry, you are forbidden from accessing this page.",21 }[props.status];22});23</script>2425<template>26 <div>27 <h1>{{ title }}</h1>28 <div>{{ description }}</div>29 </div>30</template>1<script setup>2import { computed } from "vue";34const props = defineProps({ status: Number });56const title = computed(() => {7 return {8 503: "503: Service Unavailable",9 500: "500: Server Error",10 404: "404: Page Not Found",11 403: "403: Forbidden",12 }[props.status];13});1415const description = computed(() => {16 return {17 503: "Sorry, we are doing some maintenance. Please check back soon.",18 500: "Whoops, something went wrong on our servers.",19 404: "Sorry, the page you are looking for could not be found.",20 403: "Sorry, you are forbidden from accessing this page.",21 }[props.status];22});23</script>2425<template>26 <div>27 <h1>{{ title }}</h1>28 <div>{{ description }}</div>29 </div>30</template>
1export default function ErrorPage({ status }) {2 const title = {3 503: "503: Service Unavailable",4 500: "500: Server Error",5 404: "404: Page Not Found",6 403: "403: Forbidden",7 }[status];89 const description = {10 503: "Sorry, we are doing some maintenance. Please check back soon.",11 500: "Whoops, something went wrong on our servers.",12 404: "Sorry, the page you are looking for could not be found.",13 403: "Sorry, you are forbidden from accessing this page.",14 }[status];1516 return (17 <div>18 <h1>{title}</h1>19 <div>{description}</div>20 </div>21 );22}1export default function ErrorPage({ status }) {2 const title = {3 503: "503: Service Unavailable",4 500: "500: Server Error",5 404: "404: Page Not Found",6 403: "403: Forbidden",7 }[status];89 const description = {10 503: "Sorry, we are doing some maintenance. Please check back soon.",11 500: "Whoops, something went wrong on our servers.",12 404: "Sorry, the page you are looking for could not be found.",13 403: "Sorry, you are forbidden from accessing this page.",14 }[status];1516 return (17 <div>18 <h1>{title}</h1>19 <div>{description}</div>20 </div>21 );22}
1<script>2 let { status } = $props()34 const title = {5 503: '503: Service Unavailable',6 500: '500: Server Error',7 404: '404: Page Not Found',8 403: '403: Forbidden',9 }1011 const description = {12 503: 'Sorry, we are doing some maintenance. Please check back soon.',13 500: 'Whoops, something went wrong on our servers.',14 404: 'Sorry, the page you are looking for could not be found.',15 403: 'Sorry, you are forbidden from accessing this page.',16 }17</script>1819<div>20 <h1>{title[status]}</h1>21 <div>{description[status]}</div>22</div>1<script>2 let { status } = $props()34 const title = {5 503: '503: Service Unavailable',6 500: '500: Server Error',7 404: '404: Page Not Found',8 403: '403: Forbidden',9 }1011 const description = {12 503: 'Sorry, we are doing some maintenance. Please check back soon.',13 500: 'Whoops, something went wrong on our servers.',14 404: 'Sorry, the page you are looking for could not be found.',15 403: 'Sorry, you are forbidden from accessing this page.',16 }17</script>1819<div>20 <h1>{title[status]}</h1>21 <div>{description[status]}</div>22</div>
Manual Exception Handling
Under the hood, handleExceptionsUsing() registers a $exceptions->respond() callback in your application's bootstrap/app.php file, which you may register manually if you prefer.
You may render a specific error type with a dedicated Inertia page by registering an error renderer with with_errors in src/app.rs and rendering through request.inertia().
1use Illuminate\Http\Request;2use Symfony\Component\HttpFoundation\Response;3use Inertia\Inertia;45->withExceptions(function (Exceptions $exceptions) {6 $exceptions->respond(function (Response $response, Throwable $exception, Request $request) {7 if (! app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [500, 503, 404, 403])) {8 return Inertia::render('ErrorPage', ['status' => $response->getStatusCode()])9 ->toResponse($request)10 ->setStatusCode($response->getStatusCode());11 }1213 return $response;14 });15})1use Illuminate\Http\Request;2use Symfony\Component\HttpFoundation\Response;3use Inertia\Inertia;45->withExceptions(function (Exceptions $exceptions) {6 $exceptions->respond(function (Response $response, Throwable $exception, Request $request) {7 if (! app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [500, 503, 404, 403])) {8 return Inertia::render('ErrorPage', ['status' => $response->getStatusCode()])9 ->toResponse($request)10 ->setStatusCode($response->getStatusCode());11 }1213 return $response;14 });15})
1use laravel::inertia::props;2use laravel::prelude::*;34Application::configure()5 .with_errors(|errors, _| {6 errors.render(|error: &CardDeclined, request, status| {7 let page = request8 .inertia()?9 .render("Billing/CardDeclined", props! { code => &error.code });1011 Some((status, page).into_response())12 });13 })1use laravel::inertia::props;2use laravel::prelude::*;34Application::configure()5 .with_errors(|errors, _| {6 errors.render(|error: &CardDeclined, request, status| {7 let page = request8 .inertia()?9 .render("Billing/CardDeclined", props! { code => &error.code });1011 Some((status, page).into_response())12 });13 })