Skip to content

Add top-level Fastify error handler that logs full stack on 5xx (#312)#314

Merged
melvincarvalho merged 1 commit into
gh-pagesfrom
issue-312-error-handler
Apr 23, 2026
Merged

Add top-level Fastify error handler that logs full stack on 5xx (#312)#314
melvincarvalho merged 1 commit into
gh-pagesfrom
issue-312-error-handler

Conversation

@melvincarvalho

Copy link
Copy Markdown
Contributor

Summary

  • Registers a `fastify.setErrorHandler` that logs `err.stack` plus method/url/hostname via `request.log.error` for any 5xx, so future 500s in production have a real trace in pm2/stdout instead of forcing investigators to infer the exception from the response body (see Intermittent 500 on concurrent PUTs to different trackers (pilot drag-drop) #309 for why that matters).
  • 4xx errors are untouched — they carry their own statusCode and are expected client errors.
  • Response body shape is preserved: `reply.send(err)` delegates to Fastify's default serialization, so the existing `{statusCode, error, message}` contract is unchanged.

Test plan

  • New `test/error-handler.test.js`:
    • 500 body matches Fastify default shape (no regression of the 91-byte format)
    • 500 logs include `err.stack` plus method/url/hostname
    • 4xx does NOT trigger the 5xx stack log
  • Full suite green (436/436).

Fixes #312

Without this, 500 responses carried only the 91-byte Fastify default body
and nothing hit stdout/pm2 — the #309 investigation had to infer the
exception from the response-body size. Now every 5xx logs err.stack plus
method/url/hostname via request.log.error. 4xx errors are unaffected.

Response body shape is preserved (reply.send(err) delegates to Fastify's
default serialization), so no existing consumer sees a behavior change.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a centralized Fastify error handler so unhandled 5xx responses reliably emit stack traces (with request context) to logs, improving production debugging for intermittent internal errors (per #309/#312) without changing the existing error response body contract.

Changes:

  • Introduces registerErrorHandler(fastify) that logs full err.stack + method/url/hostname for 5xx and delegates response serialization to Fastify.
  • Registers the new error handler during server creation (createServer) so it applies globally.
  • Adds a focused test verifying 5xx logging/response shape and that 4xx errors do not trigger stack logging.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
test/error-handler.test.js Adds coverage to ensure 5xx stack logging + request context and preserves Fastify default error body shape; verifies 4xx doesn’t trigger 5xx logging.
src/utils/error-handler.js Implements the top-level Fastify setErrorHandler with conditional 5xx stack logging.
src/server.js Wires the handler into server initialization so it runs for all routes/plugins.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@melvincarvalho
melvincarvalho merged commit 04218f2 into gh-pages Apr 23, 2026
4 checks passed
@melvincarvalho
melvincarvalho deleted the issue-312-error-handler branch April 23, 2026 11:33
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.

Add top-level Fastify error handler that logs full stack on 500

2 participants