inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.08k stars 234 forks source link

Error handling with unregistered route #542

Closed guratr closed 1 year ago

guratr commented 1 year ago

Hi. I have some route like this

Route::get('/page/{page}', 'PageController@show'); 

And while adding inertia error response instead of laravel error page, i noticed that if the route is not registered (for example site.com/pagethatnotinrouter), then the following code from docs example throws an error.

return Inertia::render('Error', ['status' => $response->status()])
            ->toResponse($request)
            ->setStatusCode($response->status());
thrown {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Uncaught InvalidArgumentException: View [app] not found. in /.../vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:137

And the reason is that HandleInertiaRequests as well as all other middleware won't be processed. The solution i found is to add this code in the end of my web.php file

Route::any('{any}', function () {
    abort(404);
})->where('any', '.*');

I think it's worth adding a note about this in the documentation with a (perhaps better) solution.

jessarcher commented 1 year ago

Hey there, thanks for reporting this issue.

I'm not able to replicate this locally using the example from the Inertia docs.

image

Diff from a clean install of Laravel with the Breeze Vue stack ```patch diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 56af264..c4c1eb5 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,6 +3,7 @@ namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Inertia\Inertia; use Throwable; class Handler extends ExceptionHandler @@ -27,4 +28,26 @@ public function register(): void // }); } + + /** + * Prepare exception for rendering. + * + * @return \Throwable + */ + public function render($request, Throwable $e) + { + $response = parent::render($request, $e); + + if (in_array($response->status(), [500, 503, 404, 403])) { + return Inertia::render('Error', ['status' => $response->status()]) + ->toResponse($request) + ->setStatusCode($response->status()); + } elseif ($response->status() === 419) { + return back()->with([ + 'message' => 'The page expired, please try again.', + ]); + } + + return $response; + } } diff --git a/routes/web.php b/routes/web.php index 9f834ee..a296abe 100644 --- a/routes/web.php +++ b/routes/web.php @@ -35,4 +35,8 @@ Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy'); }); +Route::get('/page/{page}', function ($page) { + dd($page); +}); + require __DIR__.'/auth.php'; ```

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

After you've posted the repository, we'll try to reproduce the issue.

Thanks!

reinink commented 1 year ago

Hey closing this one because we haven't heard back 👍