Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Exception handler not honouring Accept header #100

@JannieT

Description

@JannieT

Package version

3.1.1

Describe the bug

Our Adonis Inertia SPA also has some api endpoints. When an api client request has an Accept: application/json header we would like to return a json response. Currently the default exception handler from the official starter kit returns an html response for any 404 route.

We had to modify the exception handler to the following:

  protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {
    '404': (error, { inertia, response, request }) => {
      const accept = request.header('accept') || '';
      if (accept.includes('application/json')) {
        return response.status(404).json({ error: 'Not found' });
      }
      return inertia.render('errors/not_found', { error });
    },
    '500..599': (error, { inertia, response, request }) => {
      const accept = request.header('accept') || '';
      if (accept.includes('application/json')) {
        return response.status(500).json({ error: 'Internal server error' });
      }
      return inertia.render('errors/server_error', { error });
    },
  };

Would this be a better out of the box default for the starter kit?

Happy to put in a pull request if this is the best way to handle both SPA and api requests in the same app.

Reproduction repo

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions