fix(errors): an onError that throws degrades to the built-in renderer
`createApp({ onError })` is the advertised way to render errors, it runs on the request
path, and it ran with no boundary around it. A renderer that threw did not produce a 500
— it took the process with it, exit code 1.
This is #72 in a different callback, and easier to reach: not a cross-origin request, but
*any* request that produces an error. The stack from the reproduction goes through
`unmatchedOutcome`, so the renderer produces the 404 too — an app with a custom renderer
and no matching route was one request away from exiting.
It is also the worst-timed crash there was. The renderer only runs once something has
already gone wrong, so the failure mode read: an error occurs, the code written to report
it fails, and instead of a degraded report the server ends.
All six call sites funnel through `renderError`, so the guard goes there once. A renderer
that throws falls back to `errorToResponse` — the built-in rendering, which is exactly
what the option overrides and therefore the honest thing to fall back to. The original
error still gets its response; that is the whole point. The renderer's own failure is
logged separately and names both errors, because they are different errors and a reader
chasing the wrong one loses an afternoon.
Verified against the built `dist/` with the issue's own reproduction: `404`, process
alive, one error line naming the renderer.
`viewEngine` was checked in the same pass, since it is the other user callback on a render
path and its having a boundary was not something to assume. It has one — the pipeline's
catch covers it, so a broken engine is a 500. Asserted now rather than left to inference.
Amends the `request:failed` test added a commit ago, which asserted the crashing behaviour
as its baseline: a throwing renderer now yields the fallback's status rather than none.
The `finally` that carries the emission stays, demoted from first line of defence to
second — a failure whose reporting is what broke is the worst one to lose.
Closes #80.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
10 files changed
+153
-21
No files matched your search
+12
-6
@@ -3,6 +3,7 @@ import { renderError, type ErrorRenderer } from './transformers';
|
||||
import { isAsyncIterable } from './channel';
|
||||
import type { TransformerFn, Transport } from './metadata';
|
||||
import { TransportMismatchError } from './signals';
|
||||
import type { Logger } from './logger';
|
||||
|
||||
/** A single step in a request pipeline: a named unit whose `run` merges its output into the shared context. */
|
||||
export interface PipelineStep {
|
||||
@@ -113,9 +114,10 @@ export async function runPipeline(args: {
|
||||
bus: Bus;
|
||||
transport: Transport;
|
||||
onError?: ErrorRenderer;
|
||||
logger?: Logger;
|
||||
correlation?: Correlation;
|
||||
}): Promise<PipelineResult> {
|
||||
const { steps, handler, transformer, seed, bus, transport, onError, correlation = {} } = args;
|
||||
const { steps, handler, transformer, seed, bus, transport, onError, logger, correlation = {} } = args;
|
||||
// context is intentionally `any`: each step merges arbitrary keys into the accumulator
|
||||
let context: any = { ...seed };
|
||||
|
||||
@@ -145,6 +147,7 @@ export async function runPipeline(args: {
|
||||
error,
|
||||
{ method: req.method ?? '', url: req.url ?? '', headers: (context.headers ?? req.headers ?? {}) as never },
|
||||
onError,
|
||||
logger,
|
||||
);
|
||||
status = rendered.status;
|
||||
return rendered;
|
||||
@@ -153,11 +156,14 @@ export async function runPipeline(args: {
|
||||
// this marks the trace. A tracing exporter needs each, and only emitting the outer one is
|
||||
// what produces a trace that says a request failed without saying where.
|
||||
//
|
||||
// Emitted from `finally`, after the render rather than before it, for two reasons. The status
|
||||
// is only decided by the render — `onError` may turn any throw into any status, so deriving
|
||||
// it from the error would be a guess that a custom renderer makes wrong. And a renderer that
|
||||
// *throws* must not swallow the event: `finally` still fires, with `status` left undefined,
|
||||
// which is the one case where this event has none.
|
||||
// Emitted after the render rather than before it, because the status is only decided there:
|
||||
// `onError` may turn any throw into any status, so deriving one from the error would be a
|
||||
// guess that a custom renderer makes wrong.
|
||||
//
|
||||
// In `finally` rather than after the call, so that nothing between here and the emit can cost
|
||||
// us the only record that a request failed. `renderError` already contains a throwing
|
||||
// `onError` and degrades to the built-in rendering, so this is a second line rather than the
|
||||
// first — but a failure whose reporting is what broke is the worst one to lose.
|
||||
if (bus.hasListeners('request:failed'))
|
||||
bus.emit('request:failed', { name: correlation.route ?? 'pipeline', error, status, ...correlation });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user