Add http status to graphql errors (#5896)

Graphql errors are not properly filtered by our handler. We still
receive errors like `NOT_FOUND` in sentry while they should be filtered.
Example
[here](https://twenty-v7.sentry.io/issues/5490383016/?environment=prod&project=4507072499810304&query=is%3Aunresolved+issue.priority%3A%5Bhigh%2C+medium%5D&referrer=issue-stream&statsPeriod=7d&stream_index=6).

We associate statuses with errors in our map
`graphQLPredefinedExceptions` but we cannot retrieve the status from the
error.

This PR lists the codes that should be filtered.

To test:
- call `findDuplicates` with an invalid id
- before, server would breaks
- now the error is simply returned
This commit is contained in:
Thomas Trompette
2024-06-19 10:39:09 +02:00
committed by GitHub
parent 6fd8dab552
commit d045bcbb94
2 changed files with 63 additions and 22 deletions

View File

@ -13,6 +13,7 @@ import {
ConflictError,
MethodNotAllowedError,
TimeoutError,
ErrorCode,
} from 'src/engine/utils/graphql-errors.util';
import { ExceptionHandlerService } from 'src/engine/integrations/exception-handler/exception-handler.service';
@ -26,6 +27,17 @@ const graphQLPredefinedExceptions = {
409: ConflictError,
};
export const graphQLErrorCodesToFilter = [
ErrorCode.GRAPHQL_VALIDATION_FAILED,
ErrorCode.UNAUTHENTICATED,
ErrorCode.FORBIDDEN,
ErrorCode.NOT_FOUND,
ErrorCode.METHOD_NOT_ALLOWED,
ErrorCode.TIMEOUT,
ErrorCode.CONFLICT,
ErrorCode.BAD_USER_INPUT,
];
export const handleExceptionAndConvertToGraphQLError = (
exception: Error,
exceptionHandlerService: ExceptionHandlerService,
@ -43,6 +55,14 @@ export const shouldFilterException = (exception: Error): boolean => {
) {
return true;
}
if (
exception instanceof BaseGraphQLError &&
graphQLErrorCodesToFilter.includes(exception?.extensions?.code)
) {
return true;
}
if (exception instanceof HttpException && exception.getStatus() < 500) {
return true;
}
@ -50,7 +70,7 @@ export const shouldFilterException = (exception: Error): boolean => {
return false;
};
export const handleException = (
const handleException = (
exception: Error,
exceptionHandlerService: ExceptionHandlerService,
user?: ExceptionHandlerUser,
@ -72,7 +92,7 @@ export const convertExceptionToGraphQLError = (
return convertExceptionToGraphql(exception);
};
export const convertHttpExceptionToGraphql = (exception: HttpException) => {
const convertHttpExceptionToGraphql = (exception: HttpException) => {
const status = exception.getStatus();
let error: BaseGraphQLError;
@ -97,7 +117,10 @@ export const convertHttpExceptionToGraphql = (exception: HttpException) => {
};
export const convertExceptionToGraphql = (exception: Error) => {
const error = new BaseGraphQLError(exception.name, 'INTERNAL_SERVER_ERROR');
const error = new BaseGraphQLError(
exception.name,
ErrorCode.INTERNAL_SERVER_ERROR,
);
error.stack = exception.stack;
error.extensions['response'] = exception.message;