fix: set status code before publishing diagnostics error channel#6412
fix: set status code before publishing diagnostics error channel#6412Eomm merged 3 commits intofastify:mainfrom
Conversation
Previously, the `tracing:fastify.request.handler:error` diagnostics channel would always report `reply.statusCode` as 200 because the channel was published before the error handler had a chance to set the appropriate status code. This fix sets the status code based on the error's `statusCode` or `status` property before publishing the diagnostics channel event, so subscribers can see the correct status code. Fixes fastify#6409
|
One thing I'd like to discuss about this implementation: The condition checkI copied the exact logic from if (!reply[kReplyHasStatusCode] || reply.statusCode === 200) {However, I'm wondering if this condition might be slightly off semantically. If a user explicitly calls Technically, if the user explicitly set a status code, we probably shouldn't override it regardless of the value. So a stricter check would be: if (!reply[kReplyHasStatusCode]) {That said, the current logic matches Should I:
Happy to go either way based on your preference! |
Address reviewer feedback: - Extract repeated status code logic into reusable setErrorStatusCode function - Create lib/error-status.js to avoid circular dependency with reply.js - Simplify test inject calls using shorthand syntax
|
Thanks for the review! I've addressed your feedback:
I initially tried adding the function to The extracted function also includes a null check ( |
Summary
Fixes #6409
Previously, the
tracing:fastify.request.handler:errordiagnostics channel would always reportreply.statusCodeas 200 because the channel was published before the error handler had a chance to set the appropriate status code.This fix sets the status code based on the error's
statusCodeorstatusproperty before publishing the diagnostics channel event, so subscribers can see the correct status code.Changes
lib/handle-request.js: Set status code before publishing error channel (both sync error paths)lib/wrap-thenable.js: Set status code before publishing error channel (async error path)Test plan
diagnostics channel error event should report correct status code- verifies error with statusCodediagnostics channel error event should report 500 for errors without status- verifies plain errors default to 500diagnostics channel error event should report correct status with custom error handler- documents behavior with custom error handlers