iamolegga / nestjs-pino

Platform agnostic logger for NestJS based on Pino with REQUEST CONTEXT IN EVERY LOG
MIT License
1.24k stars 97 forks source link

Cannot read properties of undefined (reading 'raw') #1445

Open andronixmd opened 1 year ago

andronixmd commented 1 year ago

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch nestjs-pino@3.1.1 for the project I'm working on.

I faced with error "Cannot read properties of undefined (reading 'raw')". Here's the error stack:

err: {
      "type": "Error",
      "message": "Cannot read properties of undefined (reading 'raw')",
      "stack":
          TypeError: Cannot read properties of undefined (reading 'raw')
              at /usr/src/server/node_modules/nestjs-pino/LoggerErrorInterceptor.js:19:52
              at Observable.init [as _subscribe] (/usr/src/server/node_modules/rxjs/src/internal/observable/throwError.ts:125:68)
              at Observable._trySubscribe (/usr/src/server/node_modules/rxjs/src/internal/Observable.ts:244:19)
              at /usr/src/server/node_modules/rxjs/src/internal/Observable.ts:234:18
              at Object.errorContext (/usr/src/server/node_modules/rxjs/src/internal/util/errorContext.ts:29:5)
              at Observable.subscribe (/usr/src/server/node_modules/rxjs/src/internal/Observable.ts:220:5)
              at /usr/src/server/node_modules/rxjs/src/internal/operators/catchError.ts:120:25
              at OperatorSubscriber._this._error (/usr/src/server/node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:52:13)
              at OperatorSubscriber.Subscriber.error (/usr/src/server/node_modules/rxjs/src/internal/Subscriber.ts:91:12)
              at OperatorSubscriber.Subscriber._error (/usr/src/server/node_modules/rxjs/src/internal/Subscriber.ts:124:24)
}

Here is the diff that solved my problem:

diff --git a/node_modules/nestjs-pino/LoggerErrorInterceptor.js b/node_modules/nestjs-pino/LoggerErrorInterceptor.js
index a0a3b29..f9300a6 100644
--- a/node_modules/nestjs-pino/LoggerErrorInterceptor.js
+++ b/node_modules/nestjs-pino/LoggerErrorInterceptor.js
@@ -14,12 +14,14 @@ let LoggerErrorInterceptor = class LoggerErrorInterceptor {
         return next.handle().pipe((0, rxjs_1.catchError)((error) => {
             return (0, rxjs_1.throwError)(() => {
                 const response = context.switchToHttp().getResponse();
-                const isFastifyResponse = response.raw !== undefined;
-                if (isFastifyResponse) {
-                    response.raw.err = error;
-                }
-                else {
-                    response.err = error;
+                if (response) {
+                    const isFastifyResponse = response.raw !== undefined;
+                    if (isFastifyResponse) {
+                        response.raw.err = error;
+                    }
+                    else {
+                        response.err = error;
+                    }
                 }
                 return error;
             });
@@ -30,4 +32,4 @@ LoggerErrorInterceptor = __decorate([
     (0, common_1.Injectable)()
 ], LoggerErrorInterceptor);
 exports.LoggerErrorInterceptor = LoggerErrorInterceptor;
-//# sourceMappingURL=LoggerErrorInterceptor.js.map
\ No newline at end of file
+//# sourceMappingURL=LoggerErrorInterceptor.js.mapยง
\ No newline at end of file

This issue body was partially generated by patch-package.

iamolegga commented 1 year ago

Hi, thanks for opening an issue and for your kind words.

It looks like the error is faced when interceptor is used to handle non-http context errors, right?

andronixmd commented 1 year ago

This happens when an HttpException is thrown in my code like so:

throw new HttpException('Bad Request', HttpStatus.BAD_REQUEST);
iamolegga commented 1 year ago

Could you reproduce this bug in a minimal example repo?

andronixmd commented 1 year ago

I'll try