Skip to content

feat: disambiguate terminal end event and error handler thrown errors - #223

Merged
UlisesGascon merged 11 commits into
pillarjs:masterfrom
logaretm:awad/tracing-context-result
Nov 16, 2026
Merged

UlisesGascon merged 11 commits into
pillarjs:masterfrom
logaretm:awad/tracing-context-result

Conversation

@logaretm

@logaretm logaretm commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

I was double checking everything for 2.3 and testing a subscriber out in Sentry SDKs for verification and noticed two gaps that we needed to patch.

1. Terminal End Event

Layers can have sync or async handlers, so a subscriber typically cannot tell if an end event is the true end event or if there is an asyncEnd coming up. This is important for duration telemetry types like spans.

The way APMs do this today is by inspecting the context object and check for result existence using the in operator. Usually we end a span when:

if ('result' in ctx || 'error' in ctx) ctx.span.end();

Otherwise it means asyncEnd is coming up and should hold off till then. Alternatively some libs introduce a flag to tell the subscribers but I find the node semantics here reliable enough.

2. Error handlers can throw themselves

This is a gap that is exposed if we ship (1) in this PR. Because the error handler emits the same channel events as other handlers, there is no way to tell if it itself is async since error would be always set, also another gap that already existed in the initial implementation is we cannot tell if the handler itself threw or if the error was the input error forwarded by the next call.

// sync throw
app.get('/users/:id', function loadUser() { throw new Error('boom'); });
// error → end, both with { error }

// async throw
app.get('/users/:id', async function loadUser() { throw new Error('boom'); });
// end (no error yet) → error → asyncStart → asyncEnd, all after end with { error }

// error handler throws
app.use(function onError(err, req, res, next) { throw new Error('render failed'); });
// error → end, both with { incomingError: boom, error: render failed }

So in this PR, I disambiguate this by setting the forwarded error on incomingError property on the context, while error itself will be set if the handler itself throws. Making the express tracing channels fully in-line with the semantics of tracePromise/traceSync APIs.

@logaretm logaretm changed the title feat: expose the handler return value on the tracing context feat: expose the result and async flag on the tracing context Nov 13, 2026
@logaretm
logaretm marked this pull request as ready for review November 13, 2026 21:12
@logaretm
logaretm force-pushed the awad/tracing-context-result branch 2 times, most recently from 0235aba to 1517523 Compare November 13, 2026 21:23
@logaretm logaretm changed the title feat: expose the result and async flag on the tracing context feat: align the tracing context with tracePromise Nov 13, 2026
Follow tracePromise so `result` or `error` being set at `end` means no
asyncStart/asyncEnd will follow. A sync return value is the result at `end`
and a promise's resolved value the result at asyncStart. An error reported
before the handler returns is published before `end` for a sync return and
after it for a promise.

Move the error an error handler receives to `incomingError`, so `error`
only means the invocation itself failed and both can be set when an error
handler forwards a different error.
@logaretm
logaretm force-pushed the awad/tracing-context-result branch from 1517523 to 3b6e353 Compare November 13, 2026 21:26
@logaretm logaretm changed the title feat: align the tracing context with tracePromise disambiguate terminal end event and error handler thrown errors Nov 13, 2026

@UlisesGascon UlisesGascon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good and I'd like it in 2.3.0, but a sync throw that publishes no error (e.g. an error handler doing throw err) leaves end with neither result nor error, so subscribers never get a terminal signal. Can we set ctx.error there and add a test?
Also, the origin error now fires after the downstream error handler has run, and pending drops a second error. Is that intended?

@logaretm

Copy link
Copy Markdown
Member Author

All good catches, thanks! I addressed them and added tests.

@UlisesGascon UlisesGascon mentioned this pull request Nov 15, 2026
3 tasks done

@UlisesGascon UlisesGascon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @logaretm! One small thing left (non-blocking at all): the async version of the rethrow (async (err, req, res, next) => { throw err }, same for an async param callback) still reaches asyncEnd with neither result nor error, can we set ctx.error on the rejection path too?

@logaretm

Copy link
Copy Markdown
Member Author

addressed in 5a2edbc

logaretm added a commit to getsentry/js-tracing-channels-proposals that referenced this pull request Nov 15, 2026
@UlisesGascon

Copy link
Copy Markdown
Member

I plan to merge this PR tomorrow 🥳

@UlisesGascon UlisesGascon self-assigned this Nov 15, 2026
@UlisesGascon UlisesGascon changed the title disambiguate terminal end event and error handler thrown errors feat: disambiguate terminal end event and error handler thrown errors Nov 16, 2026
@UlisesGascon
UlisesGascon merged commit cc18876 into pillarjs:master Nov 16, 2026
15 of 16 checks passed
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.

2 participants