laravel icon indicating copy to clipboard operation
laravel copied to clipboard

Exception Handling with Laravel 11.x

Open LucindaX opened this issue 1 year ago • 8 comments

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

LucindaX avatar May 31 '24 12:05 LucindaX

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.

lindyhopchris avatar May 31 '24 12:05 lindyhopchris

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 avatar May 31 '24 13:05 LucindaX

@LucindaX thanks for sharing your temp. fix 👍

tdwesten avatar Jun 04 '24 10:06 tdwesten

maybe something like this: https://laravel.com/docs/11.x/errors#renderable-exceptions

rabol avatar Jun 18 '24 17:06 rabol

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.

lindyhopchris avatar Jun 19 '24 07:06 lindyhopchris

Looks good:

{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "The route api/v1/recipes/01j0qeg6245wrxg2s8wa5m1t3nss could not be found.",
            "status": "404",
            "title": "Not Found"
        }
    ]
}

rabol avatar Jun 19 '24 08:06 rabol

Great, thanks so much for confirming.

I'll leave this issue open so I don't forget to update the docs.

lindyhopchris avatar Jun 19 '24 08:06 lindyhopchris

@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?

tsterker avatar Jul 09 '24 07:07 tsterker

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.

ashatrov avatar Sep 10 '24 14:09 ashatrov

Docs are now updated: https://laraveljsonapi.io/docs/4.0/responses/errors.html#error-rendering

lindyhopchris avatar Oct 04 '24 19:10 lindyhopchris