You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was wandering, what would be the recommended way of handling errors when using Inertia with Laravel?
Here's my current solution:
In order to have consistent error pages when making requests directly and when making them through Inertia, in an app I'm using for testing Inertia, I've decided to show error pages only using Inertia. So, I have a trait that I apply to App\Exceptions\Handler class:
<?phpnamespaceApp\Exceptions;
useInertia\Inertia;
useSymfony\Component\HttpFoundation\Response;
useSymfony\Component\HttpKernel\Exception\HttpExceptionInterface;
traitShowsErrorPageUsingInertia
{
/** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e * @return \Symfony\Component\HttpFoundation\Response */protectedfunctionrenderHttpException(HttpExceptionInterface$e)
{
$statusCode = $e->getStatusCode();
$inertiaResponse = Inertia::render('Error', [
'code' => $statusCode,
'message' => $this->responseStatusText($statusCode),
]);
if (! $inertiaResponse instanceof Response) {
returnnewResponse($inertiaResponse, $statusCode);
}
return$inertiaResponse;
}
/** * Get the status text based on status code. * * @param int $statusCode * @return string */privatefunctionresponseStatusText($statusCode)
{
if ($statusCode === 419) {
return'Page Expired';
}
returnResponse::$statusTexts[$statusCode] ?? 'Unknown Error';
}
}
I could have inlined it in the class, but having this in a trait feels better to me 😄
And I have Error.vue page that shows error and short message, in the same way that Laravel does usually.
So far it's worked perfectly for me 🙂
The text was updated successfully, but these errors were encountered:
Yes, this is more or less how I would handle this. I'd update my Laravel exception handler to detect if it was an Inertia request, and then return a proper Inertia error page response. I'd probably add a check and only return that in production, because locally I like see the error in the modal.
When I try this it gives me this error: Symfony\Component\HttpFoundation\Response::__construct(): Argument #1 ($content) must be of type ?string, Inertia\Response given, called in /home/vagrant/code/schoolsysteem/app/Exceptions/ShowsErrorPageUsingInertia.php on line 21
I was wandering, what would be the recommended way of handling errors when using Inertia with Laravel?
Here's my current solution:
In order to have consistent error pages when making requests directly and when making them through Inertia, in an app I'm using for testing Inertia, I've decided to show error pages only using Inertia. So, I have a trait that I apply to
App\Exceptions\Handler
class:I could have inlined it in the class, but having this in a trait feels better to me 😄
And I have
Error.vue
page that shows error and short message, in the same way that Laravel does usually.So far it's worked perfectly for me 🙂
The text was updated successfully, but these errors were encountered: