Repository navigation
feat: disambiguate terminal end event and error handler thrown errors - #223
Conversation
0235aba to
1517523
Compare
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.
1517523 to
3b6e353
Compare
UlisesGascon
left a comment
There was a problem hiding this comment.
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?
|
All good catches, thanks! I addressed them and added tests. |
UlisesGascon
left a comment
There was a problem hiding this comment.
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?
|
addressed in 5a2edbc |
|
I plan to merge this PR tomorrow 🥳 |
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
endevent is the true end event or if there is anasyncEndcoming 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
resultexistence using theinoperator. Usually we end a span when:Otherwise it means
asyncEndis 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
errorwould be always set, also another gap that already existed in the initial implementation is we cannot tell if the handler itself threw or if theerrorwas the input error forwarded by thenextcall.So in this PR, I disambiguate this by setting the forwarded error on
incomingErrorproperty on the context, whileerroritself will be set if the handler itself throws. Making the express tracing channels fully in-line with the semantics oftracePromise/traceSyncAPIs.