Skip to content

Accessing req.routeConfig in frameworkErrors handler throws TypeError #4807

@sergburn

Description

@sergburn

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.17.0

Plugin version

No response

Node.js version

16.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.7

Description

An attempt to get a value of request.routeConfig in frameworkErrors handler throws an exception:

test/router-options.test.js 2>       return this[kRouteContext][kPublicRouteContext].config
test/router-options.test.js 2>                                                       ^
test/router-options.test.js 2> TypeError: Cannot read properties of undefined (reading 'config')

Steps to Reproduce

test('Should not throw on access to routeConfig frameworkErrors handler - FST_ERR_BAD_URL', t => {
  t.plan(6)

  const fastify = Fastify({
    frameworkErrors: function (err, req, res) {
      t.ok(typeof req.id === 'string')
      t.ok(req.raw instanceof Readable)
      t.same(req.routerPath, undefined)
      t.same(req.routeConfig, undefined)
      //         ^^^
      // test/router-options.test.js 2>       return this[kRouteContext][kPublicRouteContext].config
      // test/router-options.test.js 2>                                                       ^
      // test/router-options.test.js 2> TypeError: Cannot read properties of undefined (reading 'config')
      res.send(`${err.message} - ${err.code}`)
    }
  })

  fastify.get('/test/:id', (req, res) => {
    res.send('{ hello: \'world\' }')
  })

  fastify.inject(
    {
      method: 'GET',
      url: '/test/%world'
    },
    (err, res) => {
      t.error(err)
      t.equal(res.body, '\'/test/%world\' is not a valid url component - FST_ERR_BAD_URL')
    }
  )
})

Expected Behavior

Should return undefined as, for example, req.routerPath does

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions