A custom onError that throws takes the process down — a 404 is enough #80

Closed
opened 2026-09-01 15:26:22 +00:00 by chrnx · 0 comments
Owner

createApp({ onError }) is the documented way to render errors, and it runs on the request path with no boundary around it. A renderer that throws does not produce a 500 — it takes the process with it.

This is #72 again, in a different callback, and it is reachable more easily: the trigger is not a cross-origin request, it is any request that produces an error. Including a 404.

What happens

renderError (src/transformers.ts) is the whole of it:

return onError?.(error, req) ?? errorToResponse(error);

No try. Against the built dist/, createApp({ modules: [], onError: () => { throw new Error('renderer exploded'); } }) and one GET /nope:

Error: renderer exploded
    at onError (onerr-node.mjs:2:61)
    at renderError (dist/index.mjs:502:19)
    at errorOutcome (dist/index.mjs:1341:20)
    at unmatchedOutcome (dist/index.mjs:1375:10)
    at dispatch (dist/index.mjs:1447:12)
    at handle (dist/index.mjs:1380:12)
    at Server.<anonymous> (dist/index.mjs:1572:22)
exit=1

Node 22.22.2. On the Fetch path it rejects out of app.fetch instead, so the caller gets no response.

Note the frame above errorOutcome: unmatchedOutcome. The renderer is not reached only by a thrown handler — it renders the 404 too. An app with a custom onError and no matching route is one request away from the process ending.

Why it matters

onError is not an edge case, it is the advertised way to do this. The README sells a single hook that "covers every error response", and the docs describe RFC 7807 and content-negotiated rendering as the reasons to use it. Every one of those reads the request: headers.accept, a locale, a tenant. Reading things that a malformed request can make absent is exactly where a renderer throws — accept.split(',')[0].trim() on a header that is not there, and the deploy is gone.

It is also the worst possible time to lose the process. The renderer only runs when something already went wrong, so the failure mode is: an error occurs, the code written to report it fails, and instead of a degraded report the whole server exits.

Scope

The same shape as the #72 fix, at the one call site:

  • wrap the onError call, and on a throw fall back to errorToResponse(error) — the default rendering, which is what the option overrides and therefore the honest thing to fall back to;
  • log the renderer's failure through the app logger, naming it as the renderer's and not the request's, since the two errors are different and a reader will otherwise chase the wrong one;
  • the original error still gets its response, which is the point: a broken renderer must degrade to the built-in one, not delete the answer.

Worth checking viewEngine in the same pass — createApp({ viewEngine }) is another user callback on a render path, and whether it has a boundary is not something to assume.

Related

Found while adding a status to request:failed for #64, which had to move the emission across renderError and therefore had to ask what happens when it throws. The finally that fix uses means the event still fires — so a subscriber sees the failure even in this case. That is a consolation, not a fix: the process is still gone.

`createApp({ onError })` is the documented way to render errors, and it runs on the request path with no boundary around it. A renderer that throws does not produce a 500 — it takes the process with it. This is #72 again, in a different callback, and it is reachable more easily: the trigger is not a cross-origin request, it is *any* request that produces an error. Including a 404. ## What happens `renderError` (`src/transformers.ts`) is the whole of it: return onError?.(error, req) ?? errorToResponse(error); No try. Against the built `dist/`, `createApp({ modules: [], onError: () => { throw new Error('renderer exploded'); } })` and one `GET /nope`: Error: renderer exploded at onError (onerr-node.mjs:2:61) at renderError (dist/index.mjs:502:19) at errorOutcome (dist/index.mjs:1341:20) at unmatchedOutcome (dist/index.mjs:1375:10) at dispatch (dist/index.mjs:1447:12) at handle (dist/index.mjs:1380:12) at Server.<anonymous> (dist/index.mjs:1572:22) exit=1 Node 22.22.2. On the Fetch path it rejects out of `app.fetch` instead, so the caller gets no response. Note the frame above `errorOutcome`: **`unmatchedOutcome`**. The renderer is not reached only by a thrown handler — it renders the 404 too. An app with a custom `onError` and no matching route is one request away from the process ending. ## Why it matters `onError` is not an edge case, it is the advertised way to do this. The README sells a single hook that *"covers every error response"*, and the docs describe RFC 7807 and content-negotiated rendering as the reasons to use it. Every one of those reads the request: `headers.accept`, a locale, a tenant. Reading things that a malformed request can make absent is exactly where a renderer throws — `accept.split(',')[0].trim()` on a header that is not there, and the deploy is gone. It is also the worst possible time to lose the process. The renderer only runs when something already went wrong, so the failure mode is: an error occurs, the code written to report it fails, and instead of a degraded report the whole server exits. ## Scope The same shape as the #72 fix, at the one call site: - wrap the `onError` call, and on a throw fall back to `errorToResponse(error)` — the default rendering, which is what the option overrides and therefore the honest thing to fall back to; - log the renderer's failure through the app logger, naming it as the renderer's and not the request's, since the two errors are different and a reader will otherwise chase the wrong one; - the original error still gets its response, which is the point: a broken renderer must degrade to the built-in one, not delete the answer. Worth checking `viewEngine` in the same pass — `createApp({ viewEngine })` is another user callback on a render path, and whether it has a boundary is not something to assume. ## Related Found while adding a `status` to `request:failed` for #64, which had to move the emission across `renderError` and therefore had to ask what happens when it throws. The `finally` that fix uses means the event still fires — so a subscriber sees the failure even in this case. That is a consolation, not a fix: the process is still gone.
chrnx closed this issue 2026-09-04 13:32:29 +00:00
Sign in to join this conversation.