From 515d6fb1c5fa3a24ca8ff710dec5b979c0daa416 Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Mon, 29 Jul 2024 16:18:23 +0200 Subject: [PATCH] Filter out by error code + invert filtering (#6432) As title Instance of not working well. Co-authored-by: Charles Bochet --- .../hooks/use-graphql-error-handler.hook.ts | 16 ++++++++++------ .../generate-graphql-error-from-error.util.ts | 2 +- .../utils/should-capture-exception.util.ts | 13 +++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/twenty-server/src/engine/core-modules/graphql/hooks/use-graphql-error-handler.hook.ts b/packages/twenty-server/src/engine/core-modules/graphql/hooks/use-graphql-error-handler.hook.ts index 0f98cdcfdb42..ef47046fe5b8 100644 --- a/packages/twenty-server/src/engine/core-modules/graphql/hooks/use-graphql-error-handler.hook.ts +++ b/packages/twenty-server/src/engine/core-modules/graphql/hooks/use-graphql-error-handler.hook.ts @@ -64,12 +64,16 @@ export const useGraphQLErrorHandlerHook = < setResult, }) => { if (result.errors && result.errors.length > 0) { - const errorsToCapture = result.errors.reduce( - (acc, error) => { - if (!(error instanceof BaseGraphQLError)) { - error = generateGraphQLErrorFromError(error); - } + const originalErrors = result.errors.map((error) => { + const originalError = error.originalError; + return originalError instanceof BaseGraphQLError + ? error.originalError + : generateGraphQLErrorFromError(error); + }); + + const errorsToCapture = originalErrors.reduce( + (acc, error) => { if (shouldCaptureException(error)) { acc.push(error); } @@ -95,7 +99,7 @@ export const useGraphQLErrorHandlerHook = < errorsToCapture.map((err, i) => addEventId(err, eventIds?.[i])); } - const nonCapturedErrors = result.errors.filter( + const nonCapturedErrors = originalErrors.filter( (error) => !errorsToCapture.includes(error), ); diff --git a/packages/twenty-server/src/engine/core-modules/graphql/utils/generate-graphql-error-from-error.util.ts b/packages/twenty-server/src/engine/core-modules/graphql/utils/generate-graphql-error-from-error.util.ts index 35201e31bd73..ab351b332e28 100644 --- a/packages/twenty-server/src/engine/core-modules/graphql/utils/generate-graphql-error-from-error.util.ts +++ b/packages/twenty-server/src/engine/core-modules/graphql/utils/generate-graphql-error-from-error.util.ts @@ -14,5 +14,5 @@ export const generateGraphQLErrorFromError = (error: Error) => { graphqlError.extensions['response'] = error.message; } - return error; + return graphqlError; }; diff --git a/packages/twenty-server/src/engine/core-modules/graphql/utils/should-capture-exception.util.ts b/packages/twenty-server/src/engine/core-modules/graphql/utils/should-capture-exception.util.ts index f801e9be71ae..60ed62245f31 100644 --- a/packages/twenty-server/src/engine/core-modules/graphql/utils/should-capture-exception.util.ts +++ b/packages/twenty-server/src/engine/core-modules/graphql/utils/should-capture-exception.util.ts @@ -3,7 +3,7 @@ import { ErrorCode, } from 'src/engine/core-modules/graphql/utils/graphql-errors.util'; -export const graphQLErrorCodesToFilter = [ +export const graphQLErrorCodesToFilterOut = [ ErrorCode.GRAPHQL_VALIDATION_FAILED, ErrorCode.UNAUTHENTICATED, ErrorCode.FORBIDDEN, @@ -15,12 +15,13 @@ export const graphQLErrorCodesToFilter = [ ]; export const shouldCaptureException = (exception: Error): boolean => { - if ( - exception instanceof BaseGraphQLError && - graphQLErrorCodesToFilter.includes(exception?.extensions?.code) - ) { + if (!(exception instanceof BaseGraphQLError)) { return true; } - return false; + if (graphQLErrorCodesToFilterOut.includes(exception?.extensions?.code)) { + return false; + } + + return true; };