Skip to content

Return JSON errors without stack traces - #195

Open
NetworkTheoryAppliedResearchInstitute wants to merge 2 commits into
mainfrom
fix/json-error-handler
Open

NetworkTheoryAppliedResearchInstitute wants to merge 2 commits into
mainfrom
fix/json-error-handler

Conversation

@NetworkTheoryAppliedResearchInstitute

Copy link
Copy Markdown
Collaborator

Express's default error handler answers with an HTML page that includes the stack trace and absolute file paths whenever NODE_ENV is not production, and neither compose file sets it. A request from an origin outside the CORS allow-list reached that handler as a 500.

Changes

  • backend/middleware/errorHandler.js, registered in server.js after every route. It always answers in JSON and never includes the stack, whatever NODE_ENV is. 5xx responses carry only the status text and are logged server-side. 4xx responses show the message only when the error is marked safe to expose, as body-parser's are.
  • A refused CORS origin now gets a 403 with a JSON message instead of a 500.
  • backend/__tests__/errorHandler.test.js covers the refused origin, a malformed JSON body, an unexpected throw and an unexposed 4xx.

Tested on this branch: npm test in backend/ passes, with 65/65 node:test (4 new) and 46/46 Jest. Agrinet's CI does not run the unit tests yet, so that run was local.

Not changed: the compose files still leave NODE_ENV unset. The handler makes that setting irrelevant to error output. No binding changes, so CONFORMANCE.md is untouched.

🤖 Generated with Claude Code

Express's default error handler answers with an HTML page that includes
the stack trace and absolute file paths whenever NODE_ENV is not
"production", and neither compose file sets it. Any request from an
origin outside the CORS allow-list reached that handler as a 500.

- Add middleware/errorHandler.js, registered after every route: always
  JSON, never the stack. 5xx responses carry only the status text and
  are logged server-side; 4xx responses show the message only when the
  error is marked safe to expose (as body-parser's are).
- A refused CORS origin is now a 403 with a JSON message instead of a
  500.
- Tests cover the refused origin, a malformed JSON body, an unexpected
  throw and an unexposed 4xx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jodson Graves <info@ntari.org>
Resolve the backend/server.js conflict with #197 by taking main's
server.js and reapplying this branch's three changes: require the
JSON error handler, give the CORS refusal status 403 with an exposed
message, and register the handler after every route.

Full backend suite on the merge: node:test 64/64, Jest 65/65.

Signed-off-by: Jodson Graves <info@ntari.org>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant