Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendations for error handling? #13

Closed
nikazooz opened this issue Apr 26, 2019 · 2 comments
Closed

Recommendations for error handling? #13

nikazooz opened this issue Apr 26, 2019 · 2 comments

Comments

@nikazooz
Copy link

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:

<?php

namespace App\Exceptions;

use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

trait ShowsErrorPageUsingInertia
{
    /**
     * Render the given HttpException.
     *
     * @param  \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface  $e
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function renderHttpException(HttpExceptionInterface $e)
    {
        $statusCode = $e->getStatusCode();

        $inertiaResponse = Inertia::render('Error', [
            'code' => $statusCode,
            'message' => $this->responseStatusText($statusCode),
        ]);

        if (! $inertiaResponse instanceof Response) {
            return new Response($inertiaResponse, $statusCode);
        }

        return $inertiaResponse;
    }

    /**
     * Get the status text based on status code.
     *
     * @param  int  $statusCode
     * @return string
     */
    private function responseStatusText($statusCode)
    {
        if ($statusCode === 419) {
            return 'Page Expired';
        }

        return Response::$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 🙂

@cmizzi cmizzi mentioned this issue Apr 26, 2019
@reinink
Copy link
Member

reinink commented May 8, 2019

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.

@reinink reinink closed this as completed May 8, 2019
@Jacobtims
Copy link

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

Stack trace: https://flareapp.io/share/dmk1vJD7#F26

Am I using the wrong response class, or is there something else wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants