feat(bus): give request:failed a status, and write down the three-event overlap
One request that throws emits three events, and nothing said which of them to count. The obvious first implementation counts the failure twice — once from `request:failed` and once from the `request:end` that follows it, under whatever status it invents for the one that had none. `route:unmatched` overlaps the same way, so a 404 is two events and one outcome. Both mistakes are silent: the metrics simply lie. The division of labour is now stated on `EventPayload`, where a consumer reads it: `request:end` is terminal, universal and carries the status the client received, so it is the request counter; `request:failed` means *handler code threw*, which no status can express on its own since a rendered 422 is also a throw; `route:unmatched` means no route ran. The last two are additional, not alternatives. `request:failed` also carries a `status` now, so an error counter can break down by status without joining back through `requestId` for something the emitter already had. Getting it required moving the emission after the render rather than before, and the two reasons are worth keeping: `onError` may turn any throw into any status, so deriving one from the error would be a guess a custom renderer makes wrong — and the emission sits in `finally`, so a renderer that *throws* cannot swallow the event. That case is the only one where the event has no status, and the field's doc says so rather than leaving a reader to find out. Closes #64. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+105
-10
No files matched your search
@@ -175,6 +175,62 @@ describe('failure reporting', () => {
|
||||
expect(requestFailures).toHaveLength(1);
|
||||
expect(requestFailures[0].route).toBe('/bad/go');
|
||||
expect(requestFailures[0].requestId).toBe(stepErrors[0].requestId);
|
||||
// Carries what was actually sent, so an error counter can break down by status without
|
||||
// joining back to the `request:end` that follows it.
|
||||
expect(requestFailures[0].status).toBe(500);
|
||||
});
|
||||
|
||||
// The three-event overlap, asserted rather than described: one failing request, three events,
|
||||
// one requestId. A consumer that counts `request:failed` and `request:end` as separate outcomes
|
||||
// counts this request twice — which is the mistake the payload docs now name.
|
||||
it('emits start, failed and end for one failing request, all sharing a requestId', async () => {
|
||||
const app = createApp({ modules: [BadModule] });
|
||||
const seen: Array<{ event: string; payload: EventPayload }> = [];
|
||||
for (const event of ['request:start', 'request:failed', 'request:end'] as const)
|
||||
app.bus.on(event, (payload) => seen.push({ event, payload }));
|
||||
|
||||
await app.fetch(new Request('http://x/bad/go'));
|
||||
|
||||
expect(seen.map((s) => s.event)).toEqual(['request:start', 'request:failed', 'request:end']);
|
||||
expect(new Set(seen.map((s) => s.payload.requestId)).size).toBe(1);
|
||||
// `request:end` is the terminal one and the only one that fires for every request shape; it is
|
||||
// what a request counter counts.
|
||||
expect(seen[2].payload.status).toBe(500);
|
||||
});
|
||||
|
||||
// `onError` may turn any throw into any status, so a status derived from the error rather than
|
||||
// from the render would be a guess that a custom renderer makes wrong.
|
||||
it('reports the status a custom onError produced, not the one the error implied', async () => {
|
||||
const app = createApp({
|
||||
modules: [BadModule],
|
||||
onError: () => ({ status: 418, headers: {}, body: 'teapot' }),
|
||||
});
|
||||
const failures: EventPayload[] = [];
|
||||
app.bus.on('request:failed', (p) => failures.push(p));
|
||||
|
||||
const res = await app.fetch(new Request('http://x/bad/go'));
|
||||
|
||||
expect(res.status).toBe(418);
|
||||
expect(failures).toHaveLength(1);
|
||||
expect(failures[0].status).toBe(418);
|
||||
});
|
||||
|
||||
// A renderer that throws must not swallow the event — the failure still happened, and losing the
|
||||
// only record of it because the reporting of it broke is the worst possible trade.
|
||||
it('still reports the failure when onError itself throws, with no status to give', async () => {
|
||||
const app = createApp({
|
||||
modules: [BadModule],
|
||||
onError: () => {
|
||||
throw new Error('renderer exploded');
|
||||
},
|
||||
});
|
||||
const failures: EventPayload[] = [];
|
||||
app.bus.on('request:failed', (p) => failures.push(p));
|
||||
|
||||
await app.fetch(new Request('http://x/bad/go')).catch(() => undefined);
|
||||
|
||||
expect(failures).toHaveLength(1);
|
||||
expect(failures[0].status).toBeUndefined();
|
||||
expect((failures[0].error as Error).message).toBe('step exploded');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user