Skip to content

fix(server): add safe correlationId fallback in global error handler - #424

Open
Joyyyb wants to merge 2 commits into
Abdulazeem-code:mainfrom
Joyyyb:backend/safe-correlation-id
Open

fix(server): add safe correlationId fallback in global error handler#424
Joyyyb wants to merge 2 commits into
Abdulazeem-code:mainfrom
Joyyyb:backend/safe-correlation-id

Conversation

@Joyyyb

@Joyyyb Joyyyb commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

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.

Closes #237

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.
@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

@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.

@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@Abdulazeem-code

Copy link
Copy Markdown
Owner

Fix backend build test error

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 Defensive Checks for req.correlationId in Global Error Handler

2 participants