Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andronixmd opened this issue May 9, 2023 · 4 comments
Open

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

andronixmd opened this issue May 9, 2023 · 4 comments

Comments

@andronixmd
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] 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
Copy link
Owner

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
Copy link
Author

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

throw new HttpException('Bad Request', HttpStatus.BAD_REQUEST);

@iamolegga
Copy link
Owner

Could you reproduce this bug in a minimal example repo?

@andronixmd
Copy link
Author

I'll try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants