Replies: 2 comments
|
maybe a |
0 replies
That looks like a great idea actually! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Feature request
Is your feature request related to a problem? Please describe.
Next.js has API routes with built-in middlewares, such as
bodyParser. When an error is thrown by those middlewares, Next.js sends an error response, handled bysendError.The current implementation sends a JSON response with the error message as the body. As an example: if we send request, with
Content-Type: application/jsonwith an invalid JSON request body, the server responds withInvalid JSON.However, usually APIs that I develop have a standard envelope for errors, like:
{ "statusCode": 400, "message": "Invalid request body", "other": "optional stuff here" }I would like to be able to keep the same response envelope for built-in middleware errors.
Describe the solution you'd like
Perhaps, we could somehow be able to tweak the
sendErrorby including it in theconfigexported in API routes?Describe alternatives you've considered
I've looked into the code to see if I could somehow monkey-patch to get around the issue temporarily, but I couldn't find a way. Another solution is to turn off all the middleware and use something like micro to have full-control, but seems overkill.
Additional context
All reactions