fix(app): pass res.render errors to the error handler by v1rtl · Pull Request #537 · tinyhttp/tinyhttp · GitHub
Skip to content

fix(app): pass res.render errors to the error handler - #537

Open
v1rtl wants to merge 1 commit into
masterfrom
fix/render-error-next
Open

v1rtl wants to merge 1 commit into
masterfrom
fix/render-error-next

Conversation

@v1rtl

@v1rtl v1rtl commented Oct 20, 2026

Copy link
Copy Markdown
Member

The bug

res.render rendered with a callback that threw the error the template engine reported:

app.render(file, data, options, (err, html) => {
  if (err) throw err
  res.send(html)
})

That works when the engine calls back synchronously, since the throw unwinds into the handler and applyHandler catches it. Many engines call back asynchronously though, e.g. after reading the template file, and then the error is thrown outside of any handler. It surfaced as an uncaught exception, which terminates the process by default, and the request never got a response.

app.engine('eta', (_path, _data, _options, cb) => setImmediate(() => cb(new Error('template failed'))))
app.get('/', (_req, res) => res.render('index.eta'))

The fix

res.render passes the error to next(), so it reaches the app's error handler like any other error. This matches express, whose default res.render callback calls req.next(err).

renderTemplate takes next as a new optional last argument, extendMiddleware passes it. Without it the error is still thrown, so direct callers of renderTemplate behave as before.

Tests

tests/core/app.test.ts, new res.render suite:

  • an error of an engine calling back asynchronously reaches the error handler (an uncaught exception before)
  • an error of an engine calling back synchronously reaches the error handler

@v1rtl
v1rtl force-pushed the fix/render-error-next branch 2 times, most recently from 5e08459 to 4ec3971 Compare October 20, 2026 10:34
res.render threw the error reported by the template engine. An engine calling
back asynchronously made it an uncaught exception outside of any handler. The
error is now passed to next(), so it reaches the app's error handler, the same
way as express.
@v1rtl
v1rtl force-pushed the fix/render-error-next branch from 4ec3971 to d0c2016 Compare October 20, 2026 10:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant