-
-
Notifications
You must be signed in to change notification settings - Fork 37
Closed
Labels
Semver: MajorHas breaking changesHas breaking changesType: EnhancementImproving an existing featureImproving an existing feature
Description
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
Semver: MajorHas breaking changesHas breaking changesType: EnhancementImproving an existing featureImproving an existing feature