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

Exception Handling with Laravel 11.x #284

Closed
LucindaX opened this issue May 31, 2024 · 10 comments
Closed

Exception Handling with Laravel 11.x #284

LucindaX opened this issue May 31, 2024 · 10 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@LucindaX
Copy link

LucindaX commented May 31, 2024

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

@lindyhopchris
Copy link
Contributor

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 lindyhopchris self-assigned this May 31, 2024
@LucindaX
Copy link
Author

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 laravel/framework#51293 (comment)

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.

@tdwesten
Copy link

tdwesten commented Jun 4, 2024

@LucindaX thanks for sharing your temp. fix 👍

@rabol
Copy link

rabol commented Jun 18, 2024

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

@lindyhopchris
Copy link
Contributor

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.

@rabol
Copy link

rabol commented Jun 19, 2024

Looks good:

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

@lindyhopchris
Copy link
Contributor

Great, thanks so much for confirming.

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

@lindyhopchris lindyhopchris added the documentation Improvements or additions to documentation label Jun 26, 2024
@tsterker
Copy link

tsterker commented Jul 9, 2024

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

@lindyhopchris lindyhopchris moved this from Ready to In progress in Big Board of Everything Aug 21, 2024
@ashatrov
Copy link

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.

@lindyhopchris lindyhopchris moved this from In progress to In review in Big Board of Everything Oct 4, 2024
@lindyhopchris
Copy link
Contributor

@github-project-automation github-project-automation bot moved this from In review to Done in Big Board of Everything Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Status: Done
Development

No branches or pull requests

6 participants