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

[NestJS] Sentry Error filter doesn't work in GraphQL context #13473

Closed
Tracked by #12504
lforst opened this issue Aug 27, 2024 · 4 comments · Fixed by #13545
Closed
Tracked by #12504

[NestJS] Sentry Error filter doesn't work in GraphQL context #13473

lforst opened this issue Aug 27, 2024 · 4 comments · Fixed by #13545
Assignees
Labels
Package: nestjs Issues related to the Sentry Nestjs SDK

Comments

@lforst
Copy link
Member

lforst commented Aug 27, 2024

Hey @nicohrubec - we are using GraphQL/NestJS and it looks like the newest version of the Sentry SDK for NestJS doesn't work for this. Specifically - I think it is somewhat related to this change regressing from #12128, or this comment #12128 (comment)

It doesn't look like there is a way to specify a different base filter in the new version of the NestJS/Sentry SDK like in setupNestErrorHandler - which btw, should that be considered deprecated?

I'm wondering if SentryGlobalFilter needs to allow configuring a custom base filter here https://github.com/getsentry/sentry-javascript/blob/develop/packages/nestjs/src/setup.ts#L63 as you are able to do here

export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsErrorFilter): void {

With the current impl - we are consistently getting

{
      "query": "query {\n  triggerError\n}",
      "variables": {},
      "errors": [
        {
          "message": "response.status is not a function",
          "locations": [
            {
              "line": 2,
              "column": 3
            }
          ],
          "path": [
            "triggerError"
          ]
        }
      ]
    }

Let me know if you'd like me to open a new issue for this!

Edit - I confirmed monkey-patching the BaseExceptionFilter to ExternalExceptionFilter fixed the issue on GraphQL requests for us

diff --git a/build/cjs/setup.js b/build/cjs/setup.js
index 119735398bb49a01b1c941045a809edd45b32fdc..b06228c7d71288ef53f140de1890e9e4fefe7477 100644
--- a/build/cjs/setup.js
+++ b/build/cjs/setup.js
@@ -6,6 +6,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
 
 const common = require('@nestjs/common');
 const core = require('@nestjs/core');
+const filter = require('@nestjs/core/exceptions/external-exception-filter');
 const microservices = require('@nestjs/microservices');
 const core$1 = require('@sentry/core');
 const utils = require('@sentry/utils');
@@ -47,7 +48,7 @@ common.Injectable()(SentryTracingInterceptor);
 /**
  * Global filter to handle exceptions and report them to Sentry.
  */
-class SentryGlobalFilter extends core.BaseExceptionFilter {
+class SentryGlobalFilter extends filter.ExternalExceptionFilter {
    static  __initStatic2() {this.__SENTRY_INTERNAL__ = true;}
 
   /**

Originally posted by @ysuhaas in #12351

@lforst lforst mentioned this issue Aug 27, 2024
@nicohrubec nicohrubec added the Package: nestjs Issues related to the Sentry Nestjs SDK label Aug 28, 2024
@nicohrubec
Copy link
Contributor

nicohrubec commented Aug 28, 2024

For now I think the solution here should be to just add an additional variation of the SentryGlobalFilter that can handle GraphQL contexts (extending ExternalExceptionFilter). However, with this setup users might still run into issues in certain scenarios as was already discussed before. ref In these cases custom filters are needed. Currently the way to work around this is to use the WithSentry() decorator.

In the future, we can think about providing a factory-method that takes an arbitrary filter and proxies it with some logic to report errors. Instead of useClass developers can then use useFactory to add the proxied filter to their application.

@ralexandr
Copy link

I've met same issue, but withSentry could not resolve it
When i add withSentry decorator to my custom AllExceptionsFilter, "exception" and "host" variables are undefined.

 async catch(exception, host) {
    console.log(exception, host);  // This will return "undefined undefined" to stdout
}

@nicohrubec
Copy link
Contributor

@ralexandr Did you put the decorator on the catch directly or on top of the AllExceptionsFilter?

@nicohrubec
Copy link
Contributor

Hey everyone, we added a new SentryGlobalGraphQLFilter, which is essentially a reimplementation of the NestJS ExternalExceptionFilter. This filter should be used in NestJS + GraphQL applications.

In our e2e sample applications, this seems to resolve this issue, but let us know if you run into any further troubles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nestjs Issues related to the Sentry Nestjs SDK
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants