fix(server): add safe correlationId fallback in global error handler - #424
Open
Joyyyb wants to merge 2 commits into
Open
fix(server): add safe correlationId fallback in global error handler#424Joyyyb wants to merge 2 commits into
Joyyyb wants to merge 2 commits into
Conversation
Issue closed: safe correlation ID fallback in global error handler
Problem
-------
The global error handler (app.use((err, req, res, next) => { ... })) in
stellar-payment-platform/server.js logged and returned req.correlationId
directly. If an error is thrown before the correlation-ID middleware has
had a chance to attach that property to the request object, the value is
undefined. This caused console.error to print '[Trace ID: undefined]' and
the JSON response body to contain { trace_id: undefined }, which is
stripped by JSON serialisation, leaving clients with no trace reference
at all.
Root Cause
----------
No defensive guard existed around req.correlationId before it was used.
Express does not guarantee that every middleware has run by the time an
error propagates to the error-handling layer, so req.correlationId can
legitimately be absent.
Fix Applied (stellar-payment-platform/server.js, ~line 372)
------------------------------------------------------------
Added a single fallback assignment immediately before the value is used:
const traceId = req.correlationId || 'unknown';
Then updated both call sites that previously referenced req.correlationId:
1. console.error(`[Error ID: ${errorId}] [Trace ID: ${traceId}]`, err);
2. JSON response body now includes: trace_id: traceId
This means:
- When the correlation middleware has already run, the real request ID
is preserved and surfaced as before.
- When the middleware has not yet run (or the property is falsy for any
other reason), the string 'unknown' is used instead of undefined,
keeping log lines and API responses consistent and parseable.
No other logic was changed. The existing reference_id (crypto.randomUUID)
that uniquely identifies each error occurrence is retained alongside the
new trace_id field.
Acceptance Criteria Met
-----------------------
- The global error handler safely defaults to 'unknown' when
req.correlationId is falsy.
- console.error and the JSON response body both use the same traceId
variable, ensuring they are always in sync.
|
@Joyyyb is attempting to deploy a commit to the Abdulazeem's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@Joyyyb Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Owner
|
Fix backend build test error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue closed: safe correlation ID fallback in global error handler
Problem
The global error handler (app.use((err, req, res, next) => { ... })) in stellar-payment-platform/server.js logged and returned req.correlationId directly. If an error is thrown before the correlation-ID middleware has had a chance to attach that property to the request object, the value is undefined. This caused console.error to print '[Trace ID: undefined]' and the JSON response body to contain { trace_id: undefined }, which is stripped by JSON serialisation, leaving clients with no trace reference at all.
Root Cause
No defensive guard existed around req.correlationId before it was used. Express does not guarantee that every middleware has run by the time an error propagates to the error-handling layer, so req.correlationId can legitimately be absent.
Fix Applied (stellar-payment-platform/server.js, ~line 372) ------------------------------------------------------------ Added a single fallback assignment immediately before the value is used:
const traceId = req.correlationId || 'unknown';
Then updated both call sites that previously referenced req.correlationId:
[Error ID: ${errorId}] [Trace ID: ${traceId}], err);This means:
No other logic was changed. The existing reference_id (crypto.randomUUID) that uniquely identifies each error occurrence is retained alongside the new trace_id field.
Acceptance Criteria Met
Closes #237