Skip to content

feat: send error stacks in responses when NODE_ENV is development - #503

Open
officialasishkumar wants to merge 1 commit into
vercel:mainfrom
officialasishkumar:feat/development-error-stacks
Open

officialasishkumar wants to merge 1 commit into
vercel:mainfrom
officialasishkumar:feat/development-error-stacks

Conversation

@officialasishkumar

Copy link
Copy Markdown

Closes #502

The README says error stacks are sent in responses when NODE_ENV is development. micro 9.x did that in sendError(), but the TypeScript rewrite (#458) dropped it together with test/development.js, so in 10.x NODE_ENV has no effect on error responses.

Changes

  • When NODE_ENV is development, sendError() sends error.stack as the body. The status code doesn't change: it's error.statusCode if set, otherwise 500.
  • Outside development, nothing changes: errors with a statusCode send error.message, and other errors send Internal Server Error.
  • NODE_ENV is read on each call rather than when the module loads, so tests can toggle it.
  • Adds a changeset.

How to test

// index.js
module.exports = () => {
  throw new Error('Custom');
};
NODE_ENV=development micro index.js
curl localhost:3000   # Error: Custom\n    at module.exports (...)

New tests in test/suite/index.ts cover the stack in development, with and without a statusCode, and the unchanged bodies in production. Without the change, both development tests fail. pnpm test, type-check and eslint-check pass.

The README documents that error stacks are sent in responses when
NODE_ENV is set to 'development'. micro 9.x implemented this in
sendError(), but the TypeScript rewrite dropped the behaviour together
with its tests, so in 10.x NODE_ENV has no effect on error responses.

sendError() now sends `error.stack` as the response body when NODE_ENV
is 'development', keeping the same status code as before (the error's
statusCode, or 500). If the error has no stack, the usual body is sent.
Outside of development, responses are unchanged: the error message for
errors with a statusCode, and 'Internal Server Error' otherwise.

NODE_ENV is read on each call so it can be changed at runtime and in
tests.

Tests cover the stack being sent in development with and without a
statusCode, and the message/'Internal Server Error' body in production.

Closes vercel#502
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error stacks are no longer sent in responses when NODE_ENV is development

1 participant