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
@@ -80,3 +80,76 @@ describe('HttpError body', () => {
|
||||
server.close();
|
||||
});
|
||||
});
|
||||
|
||||
// `onError` is user code, on the request path, running *after something already went wrong*. An
|
||||
// unguarded throw here was the worst-timed crash in the framework: the error occurs, the code
|
||||
// written to report it fails, and the process ends instead of degrading. It was also easy to
|
||||
// reach — the renderer produces the 404 too, so an app with a custom renderer and no matching
|
||||
// route was one request away from exiting.
|
||||
describe('an onError that throws', () => {
|
||||
const exploding: ErrorRenderer = () => {
|
||||
throw new Error('renderer exploded');
|
||||
};
|
||||
|
||||
@Route('/x')
|
||||
class Boom {
|
||||
@Get('/boom') boom() {
|
||||
throw new HttpError(503, 'Service Unavailable');
|
||||
}
|
||||
}
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [Boom] })
|
||||
class BoomModule {}
|
||||
|
||||
it('falls back to the built-in rendering rather than losing the response', async () => {
|
||||
const app = createApp({ modules: [BoomModule], onError: exploding });
|
||||
const res = await app.fetch(new Request('http://x/x/boom'));
|
||||
|
||||
// The original error still gets its answer: the fallback is the rendering `onError` overrides.
|
||||
expect(res.status).toBe(503);
|
||||
expect(await res.json()).toEqual({ error: 'Service Unavailable' });
|
||||
});
|
||||
|
||||
// The path that needed no thrown handler at all.
|
||||
it('survives a 404, which the renderer also produces', async () => {
|
||||
const app = createApp({ modules: [BoomModule], onError: exploding });
|
||||
const res = await app.fetch(new Request('http://x/nothing-here'));
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(await res.json()).toEqual({ error: 'Not Found' });
|
||||
});
|
||||
|
||||
it('reports the renderer as the thing that failed, not the request', async () => {
|
||||
const logged: Array<{ msg: string; fields?: Record<string, unknown> }> = [];
|
||||
const logger = {
|
||||
debug() {},
|
||||
info() {},
|
||||
warn() {},
|
||||
error(msg: string, fields?: Record<string, unknown>) {
|
||||
logged.push({ msg, fields });
|
||||
},
|
||||
};
|
||||
const app = createApp({ modules: [BoomModule], onError: exploding, logger });
|
||||
|
||||
await app.fetch(new Request('http://x/x/boom'));
|
||||
|
||||
expect(logged).toHaveLength(1);
|
||||
expect(logged[0].msg).toMatch(/onError/);
|
||||
// Both errors named, because a reader chasing the wrong one loses an afternoon.
|
||||
expect(logged[0].fields?.err).toBe('renderer exploded');
|
||||
expect(logged[0].fields?.rendering).toBe('Service Unavailable');
|
||||
});
|
||||
|
||||
it('answers over a real Node server instead of exiting the process', async () => {
|
||||
const app = createApp({ modules: [BoomModule], onError: exploding });
|
||||
const server = await app.listen(0);
|
||||
const port = (server.address() as { port: number }).port;
|
||||
|
||||
const res = await fetch(`http://127.0.0.1:${port}/x/boom`);
|
||||
expect(res.status).toBe(503);
|
||||
// Still serving — the point of the whole test.
|
||||
expect((await fetch(`http://127.0.0.1:${port}/nope`)).status).toBe(404);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user