laravel
laravel copied to clipboard
Exception Handling with Laravel 11.x
Exception handling is mentioned in the installation portion of the tutorial-app https://laraveljsonapi.io/docs/3.0/getting-started/#exception-handler
In Laravel 11.x that default Handler is removed, and instead bootstrap/app.php is supposed to be used as in
->withExceptions(function (Exceptions $exceptions) {
$exceptions->report(function (InvalidOrderException $e) { // from laravel 11.x docs example
// ...
});
})
How do I set up the laravel-json-api Exception Handling in laravel 11.x
Thanks for reporting, I hadn't realised they'd removed the app's exception handler. I'll have to look into this as I don't know the answer without looking at how they now expect you to register exception renderers now.
To make it work currently I re-created app/Exceptions/Handler.php and copied the file contents as in https://laraveljsonapi.io/docs/3.0/getting-started/#exception-handler
And in bootstrap/app.php I added
->withSingletons([
\Illuminate\Contracts\Debug\ExceptionHandler::class => \App\Exceptions\Handler::class,
])
This makes it return the Exceptions in json:API format. Not sure if that's the right way to go about it tho. Got the reference from https://github.com/laravel/framework/pull/51293#issue-2279268606
As an example, when querying a resource with a non-eisting id
{
"jsonapi": {
"version": "1.0"
},
"errors": [
{
"detail": "The route api/v1/posts/89 could not be found.",
"status": "404",
"title": "Not Found"
}
]
}
It seems it's returning detail for the Symfony\Component\HttpKernel\Exception\NotFoundHttpException as the base, I assume there is a previous error that is being throw that would indicate a "Resource not found".
I'm not sure if that's the correct behavior or not.
@LucindaX thanks for sharing your temp. fix 👍
maybe something like this: https://laravel.com/docs/11.x/errors#renderable-exceptions
Was looking at this the other day. This looks like the solution: https://laravel.com/docs/11.x/errors#rendering-exceptions
->withExceptions(function (Exceptions $exceptions) {
$exceptions->render(
\LaravelJsonApi\Exceptions\ExceptionParser::renderer(),
);
})
Would be good if someone could try that as my apps are still on Laravel 10.
Looks good:
{
"jsonapi": {
"version": "1.0"
},
"errors": [
{
"detail": "The route api/v1/recipes/01j0qeg6245wrxg2s8wa5m1t3nss could not be found.",
"status": "404",
"title": "Not Found"
}
]
}
Great, thanks so much for confirming.
I'll leave this issue open so I don't forget to update the docs.
@lindyhopchris For the record, I'm using this configuration and it also seems to work so far:
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontReport([
JsonApiException::class,
]);
$exceptions->renderable(\LaravelJsonApi\Exceptions\ExceptionParser::make()->renderable());
})
I have not looked into this further and maybe this is just more verbose/redundant?
I've spent 3 hours investigating why errors are not working! Last comment from tsterker works well and seems compatible with Laravel 11 documentation.
It is only required changes in the documentation of the plugin.
Docs are now updated: https://laraveljsonapi.io/docs/4.0/responses/errors.html#error-rendering