express-winston icon indicating copy to clipboard operation
express-winston copied to clipboard

Issue with `errorLogger` msg always returning `200` for `res.statusCode`

Open dkarkar92 opened this issue 2 years ago • 0 comments

Versions:

  • express-winston: "4.2.0"
  • winston: "3.8.2"

I have a expressWinston.logger and expressWinston.errorLogger setup currently. If my application throws an error, say 403 for instance, the expressWinston.logger will log that correctly. Example:

{
  meta: {},
  level: 'info',
  message: 'PATCH /xyz/123 403 405ms',
  timestamp: '2022-10-13T17:06:57.871Z',
}

The issue is that the errorLogger does not correctly report the statusCode properly. It will always return 200 rendering the message {{res.statusCode}} field useless for me. See:

{
  meta: {
    error: .../serverError.ts:25
      return new Exception(errObj.status, errObj.message, errObj.errorCode)
             ^

    Error: User does not have permissions to create or modify users
      ...
      status: 403,
      errorCode: '70060'
    },
    level: 'error',
    message: 'uncaughtException: User does not have permissions to create or modify users\n ...'
    exception: true,
    date: 'Thu Oct 13 2022 17:06:57 GMT+0000 (Coordinated Universal Time)',
    process: {...},
    os: { ... },
    trace: [...],
    req: {
      url: '...,
      headers: {...},
      method: 'PATCH',
      httpVersion: '1.1',
      originalUrl: '...',
      query: {}
    }
  },
  level: '\x1B[31merror\x1B[39m',
  message: 'User does not have permissions to create or modify users - 200 PATCH ...'
}

and the config for the errorLogger (note this isn't my live prod code, just something I modified to reproduce the error in my app):

  app.use(errorLogger({
    transports: [
      new transports.Console()
    ],
    level: 'error',
    msg: `{{err.message}} - {{res.statusCode}} {{req.method}} {{req.url}}`,
    format: format.combine(
      format.colorize(),
      format.json(),
      format.prettyPrint(),
    )
  }));

Question: Is this a known issue? Or is there something wrong my my config? Or is there an issue with how my error is being thrown? What am I missing? I want my errorLogger to accurately report the 403 error.

dkarkar92 avatar Oct 13 '22 17:10 dkarkar92