Skip to content

Inconsistent logger call in frameworksError wrapper #4808

@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

The wrapper for frameworkErrors custom handler in fastify.js, onBadUrl() supplies raw request to the "incoming request" logger, unlike in normal logging case in route.js.

This breaks input expectation for custom logger's request serializer - normally req argument is Fastify request, but in this case it is raw request. This can lead to an exception.

BTW, it also doesn't honor disableRequestLogging option in this case.

Same is true for onAsyncConstraintError framework error case.

See also #4807

Steps to Reproduce

test('Should supply Fastify request to the logger in frameworkErrors wrapper - FST_ERR_BAD_URL',  t => {
  t.plan(5)

  const logStream = split(JSON.parse)

  const fastify = Fastify({
    frameworkErrors: function (err, req, res) {
      res.send(`${err.message} - ${err.code}`)
    },
    logger: {
      stream: logStream,
      serializers: {
        req (request) {
          return { httpVersion: request.raw.httpVersion }
          //                            ^^^
          // test/router-options.test.js 2> TypeError: Cannot read properties of undefined (reading 'httpVersion')
        }
      }
    },
    genReqId: () => 'REQ-1234'
  })

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

  logStream.on('data', (json) => {
    console.log(json)
    t.same(json.msg, 'incoming request')
    t.same(json.reqId, 'REQ-1234')
    t.same(json.httpVersion, '1.1')
  })

  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 supply Fastify request object

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions