Skip to content

Error tracing on router.param #213

Closed

Description

@jonchurch

Is it intended for errors that originate in router.param() callbacks to be observable by the diagnostic channel? ref #196

Currently with this code below, which admittedly is a bit contrived, the error originating in validateId would be attributed to the forwarding error handler:

const Router = require('./index.js')
const dc = require('node:diagnostics_channel')
const http = require('node:http')

const router = Router()

dc.tracingChannel('express.router.request').subscribe({
  error(ctx) {
    console.log('Error attributed to:', ctx.layer.name)
  }
})

router.param('id', function validateId(req, res, next) {
  next(new Error('Invalid ID'))
})

router.get('/users/:id', (req, res) => {
  res.end('OK')
})

router.use(function forwarding(err, req, res, next) {
  next(err)
})

router.use(function recovering(err, req, res, next) {
  res.statusCode = 400
  res.end(err.message)
})

const server = http.createServer((req, res) => {
  router(req, res, (err) => {
    res.statusCode = err ? 500 : 404
    res.end(err?.message || 'Not found')
  })
})

server.listen(0, async () => {
  const url = `http://localhost:${server.address().port}/users/123`

  try {
    const response = await fetch(url)
    console.log('HTTP status:', response.status)
  } finally {
    server.close()
  }
})

Actual output:

Error attributed to: forwarding

It seems that router.param() callbacks bypass the instrumented layer handlers, so their errors aren't observed until a subsequent error handler forwards them via next(err).

I'm wondering whether we should capture these errors at their origin, or at least avoid incorrectly attributing them to a subsequent handler.

Activity

  1. changed the title [-]Error tracing on `req.params`[/-] [+]Error tracing on `router.param`[/+] on Oct 21, 2026
  2. bjohansebas commented on Oct 23, 2026

    @bjohansebas
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions