Spaces:
Build error
Build error
File size: 4,443 Bytes
d9494a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import { HttpException } from '@nestjs/common';
import { GraphQLError } from 'graphql';
import { type ExceptionHandlerUser } from 'src/engine/core-modules/exception-handler/interfaces/exception-handler-user.interface';
import { type ExceptionHandlerWorkspace } from 'src/engine/core-modules/exception-handler/interfaces/exception-handler-workspace.interface';
import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';
import { type ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service';
import {
AuthenticationError,
BaseGraphQLError,
ConflictError,
ErrorCode,
ForbiddenError,
MethodNotAllowedError,
NotFoundError,
TimeoutError,
ValidationError,
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import { type CustomException } from 'src/utils/custom-exception';
const graphQLPredefinedExceptions = {
400: ValidationError,
401: AuthenticationError,
403: ForbiddenError,
404: NotFoundError,
405: MethodNotAllowedError,
408: TimeoutError,
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,
ErrorCode.METADATA_VALIDATION_FAILED,
];
export const handleExceptionAndConvertToGraphQLError = (
exception: Error,
exceptionHandlerService: ExceptionHandlerService,
user?: ExceptionHandlerUser,
workspace?: ExceptionHandlerWorkspace,
): BaseGraphQLError => {
handleException({
exception,
exceptionHandlerService,
user,
workspace,
});
return convertExceptionToGraphQLError(exception);
};
export const shouldCaptureException = (
exception: Error,
statusCode?: number,
): boolean => {
if (
exception instanceof GraphQLError &&
(exception?.extensions?.http?.status ?? 500) < 500
) {
return false;
}
if (
exception instanceof BaseGraphQLError &&
graphQLErrorCodesToFilter.includes(exception?.extensions?.code)
) {
return false;
}
if (exception instanceof HttpException && exception.getStatus() < 500) {
return false;
}
if (statusCode && statusCode < 500) {
return false;
}
return true;
};
export const handleException = <
T extends Error | CustomException | HttpException,
>({
exception,
exceptionHandlerService,
user,
workspace,
statusCode,
shouldBeCapturedBySentry = true,
}: {
exception: T;
exceptionHandlerService: ExceptionHandlerService;
user?: ExceptionHandlerUser;
workspace?: ExceptionHandlerWorkspace;
statusCode?: number;
shouldBeCapturedBySentry?: boolean;
}): T => {
if (
shouldBeCapturedBySentry &&
shouldCaptureException(exception, statusCode)
) {
exceptionHandlerService.captureExceptions([exception], { user, workspace });
}
return exception;
};
export const convertExceptionToGraphQLError = (
exception: Error,
): BaseGraphQLError => {
if (exception instanceof HttpException) {
return convertHttpExceptionToGraphql(exception);
}
if (exception instanceof BaseGraphQLError) {
return exception;
}
return convertExceptionToGraphql(exception);
};
const convertHttpExceptionToGraphql = (exception: HttpException) => {
const status = exception.getStatus();
let error: BaseGraphQLError;
if (status in graphQLPredefinedExceptions) {
// @ts-expect-error legacy noImplicitAny
const message = exception.getResponse()['message'] ?? exception.message;
// @ts-expect-error legacy noImplicitAny
error = new graphQLPredefinedExceptions[exception.getStatus()](message);
} else {
error = new BaseGraphQLError(
'Internal Server Error',
exception.getStatus().toString(),
);
}
// Only show the stack trace in development mode
if (process.env.NODE_ENV === NodeEnvironment.DEVELOPMENT) {
error.stack = exception.stack;
error.extensions['response'] = exception.getResponse();
}
return error;
};
export const convertExceptionToGraphql = (exception: Error) => {
const error = new BaseGraphQLError(
'Internal Server Error',
ErrorCode.INTERNAL_SERVER_ERROR,
);
if (process.env.NODE_ENV === NodeEnvironment.DEVELOPMENT) {
error.stack = exception.stack;
error.extensions['response'] = exception.message;
}
return error;
};
|