Improve error handling (#13130)
In the BE we throw custom errors with precise error codes (e.g. "LABEL_ALREADY_EXISTS") before catching them in filters and rethrowing BaseGraphQLErrors (standard errors such as NotFoundError, UserInputError etc.). In the FE we were grouping sentries based on the error codes but we were actually grouping by very broad codes such as "NOT_FOUND" or "BAD_USER_INPUT", extracted from the BaseGraphQLErrors. To fix that, we update the BaseGraphQLError constructor api to allow to pass on the CustomError directly and retrieve from it the original code and store it in existing property `subCode` that we will use in the FE to send errors to sentry. This new api also eases usage of `userFriendlyMessage` that is passed on to the api response and therefore to the FE when CustomError is passed on directly to the BaseGraphQLError constructor.
This commit is contained in:
@ -17,9 +17,7 @@ export class ApprovedAccessDomainExceptionFilter implements ExceptionFilter {
|
||||
case ApprovedAccessDomainExceptionCode.APPROVED_ACCESS_DOMAIN_VALIDATION_TOKEN_INVALID:
|
||||
case ApprovedAccessDomainExceptionCode.APPROVED_ACCESS_DOMAIN_ALREADY_VALIDATED:
|
||||
case ApprovedAccessDomainExceptionCode.APPROVED_ACCESS_DOMAIN_MUST_BE_A_COMPANY_DOMAIN:
|
||||
throw new ForbiddenError(exception.message, {
|
||||
userFriendlyMessage: exception.userFriendlyMessage,
|
||||
});
|
||||
throw new ForbiddenError(exception);
|
||||
default: {
|
||||
const _exhaustiveCheck: never = exception.code;
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ export class AuditExceptionFilter implements ExceptionFilter {
|
||||
switch (exception.code) {
|
||||
case AuditExceptionCode.INVALID_TYPE:
|
||||
case AuditExceptionCode.INVALID_INPUT:
|
||||
throw new UserInputError(exception.message);
|
||||
throw new UserInputError(exception);
|
||||
default: {
|
||||
const _exhaustiveCheck: never = exception.code;
|
||||
|
||||
|
||||
@ -18,13 +18,9 @@ export class AuthGraphqlApiExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: AuthException) {
|
||||
switch (exception.code) {
|
||||
case AuthExceptionCode.CLIENT_NOT_FOUND:
|
||||
throw new NotFoundError(exception.message, {
|
||||
userFriendlyMessage: exception.userFriendlyMessage,
|
||||
});
|
||||
throw new NotFoundError(exception);
|
||||
case AuthExceptionCode.INVALID_INPUT:
|
||||
throw new UserInputError(exception.message, {
|
||||
userFriendlyMessage: exception.userFriendlyMessage,
|
||||
});
|
||||
throw new UserInputError(exception);
|
||||
case AuthExceptionCode.FORBIDDEN_EXCEPTION:
|
||||
case AuthExceptionCode.INSUFFICIENT_SCOPES:
|
||||
case AuthExceptionCode.OAUTH_ACCESS_DENIED:
|
||||
@ -33,13 +29,12 @@ export class AuthGraphqlApiExceptionFilter implements ExceptionFilter {
|
||||
case AuthExceptionCode.SIGNUP_DISABLED:
|
||||
case AuthExceptionCode.MISSING_ENVIRONMENT_VARIABLE:
|
||||
case AuthExceptionCode.INVALID_JWT_TOKEN_TYPE:
|
||||
throw new ForbiddenError(exception.message, {
|
||||
userFriendlyMessage: exception.userFriendlyMessage,
|
||||
});
|
||||
throw new ForbiddenError(exception);
|
||||
case AuthExceptionCode.GOOGLE_API_AUTH_DISABLED:
|
||||
case AuthExceptionCode.MICROSOFT_API_AUTH_DISABLED:
|
||||
throw new ForbiddenError(exception.message, {
|
||||
userFriendlyMessage: t`Authentication is not enabled with this provider.`,
|
||||
subCode: exception.code,
|
||||
});
|
||||
case AuthExceptionCode.EMAIL_NOT_VERIFIED:
|
||||
case AuthExceptionCode.INVALID_DATA:
|
||||
@ -50,10 +45,11 @@ export class AuthGraphqlApiExceptionFilter implements ExceptionFilter {
|
||||
case AuthExceptionCode.UNAUTHENTICATED:
|
||||
throw new AuthenticationError(exception.message, {
|
||||
userFriendlyMessage: t`You must be authenticated to perform this action.`,
|
||||
subCode: exception.code,
|
||||
});
|
||||
case AuthExceptionCode.USER_NOT_FOUND:
|
||||
case AuthExceptionCode.WORKSPACE_NOT_FOUND:
|
||||
throw new AuthenticationError(exception.message);
|
||||
throw new AuthenticationError(exception);
|
||||
case AuthExceptionCode.INTERNAL_SERVER_ERROR:
|
||||
case AuthExceptionCode.USER_WORKSPACE_NOT_FOUND:
|
||||
throw exception;
|
||||
|
||||
@ -23,13 +23,9 @@ export class EmailVerificationExceptionFilter implements ExceptionFilter {
|
||||
case EmailVerificationExceptionCode.INVALID_TOKEN:
|
||||
case EmailVerificationExceptionCode.INVALID_APP_TOKEN_TYPE:
|
||||
case EmailVerificationExceptionCode.RATE_LIMIT_EXCEEDED:
|
||||
throw new ForbiddenError(exception.message, {
|
||||
subCode: exception.code,
|
||||
});
|
||||
throw new ForbiddenError(exception);
|
||||
case EmailVerificationExceptionCode.EMAIL_MISSING:
|
||||
throw new UserInputError(exception.message, {
|
||||
subCode: exception.code,
|
||||
});
|
||||
throw new UserInputError(exception);
|
||||
case EmailVerificationExceptionCode.EMAIL_ALREADY_VERIFIED:
|
||||
throw new UserInputError(exception.message, {
|
||||
subCode: exception.code,
|
||||
@ -41,9 +37,7 @@ export class EmailVerificationExceptionFilter implements ExceptionFilter {
|
||||
userFriendlyMessage: t`Email verification not required.`,
|
||||
});
|
||||
case EmailVerificationExceptionCode.INVALID_EMAIL:
|
||||
throw new UserInputError(exception.message, {
|
||||
subCode: exception.code,
|
||||
});
|
||||
throw new UserInputError(exception);
|
||||
default: {
|
||||
const _exhaustiveCheck: never = exception.code;
|
||||
|
||||
|
||||
@ -6,6 +6,8 @@ import {
|
||||
SourceLocation,
|
||||
} from 'graphql';
|
||||
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
declare module 'graphql' {
|
||||
export interface GraphQLErrorExtensions {
|
||||
exception?: {
|
||||
@ -48,29 +50,42 @@ export class BaseGraphQLError extends GraphQLError {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
[key: string]: any;
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
exceptionOrMessage: string | CustomException,
|
||||
code?: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
extensions?: Record<string, any>,
|
||||
) {
|
||||
super(message);
|
||||
if (exceptionOrMessage instanceof CustomException) {
|
||||
const exception = exceptionOrMessage;
|
||||
|
||||
super(exception.message);
|
||||
|
||||
this.extensions = {
|
||||
subCode: exception.code,
|
||||
userFriendlyMessage: exception.userFriendlyMessage,
|
||||
code,
|
||||
};
|
||||
} else {
|
||||
const message = exceptionOrMessage;
|
||||
|
||||
super(message);
|
||||
|
||||
if (extensions?.extensions) {
|
||||
throw new Error(
|
||||
'Pass extensions directly as the third argument of the ApolloError constructor: `new ' +
|
||||
'ApolloError(message, code, {myExt: value})`, not `new ApolloError(message, code, ' +
|
||||
'{extensions: {myExt: value}})`',
|
||||
);
|
||||
}
|
||||
|
||||
this.extensions = { ...extensions, code };
|
||||
}
|
||||
|
||||
// if no name provided, use the default. defineProperty ensures that it stays non-enumerable
|
||||
if (!this.name) {
|
||||
Object.defineProperty(this, 'name', { value: 'GraphQLError' });
|
||||
}
|
||||
|
||||
if (extensions?.extensions) {
|
||||
throw new Error(
|
||||
'Pass extensions directly as the third argument of the ApolloError constructor: `new ' +
|
||||
'ApolloError(message, code, {myExt: value})`, not `new ApolloError(message, code, ' +
|
||||
'{extensions: {myExt: value}})`',
|
||||
);
|
||||
}
|
||||
|
||||
this.extensions = { ...extensions, code };
|
||||
}
|
||||
|
||||
toJSON(): GraphQLFormattedError {
|
||||
@ -113,28 +128,62 @@ export class ValidationError extends BaseGraphQLError {
|
||||
}
|
||||
}
|
||||
|
||||
export class AuthenticationError extends BaseGraphQLError {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions) {
|
||||
super(message, ErrorCode.UNAUTHENTICATED, extensions);
|
||||
export class NotFoundError extends BaseGraphQLError {
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.NOT_FOUND, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'NotFoundError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class AuthenticationError extends BaseGraphQLError {
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.UNAUTHENTICATED, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'AuthenticationError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class ForbiddenError extends BaseGraphQLError {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions) {
|
||||
super(message, ErrorCode.FORBIDDEN, extensions);
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.FORBIDDEN, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'ForbiddenError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class PersistedQueryNotFoundError extends BaseGraphQLError {
|
||||
constructor() {
|
||||
super('PersistedQueryNotFound', ErrorCode.PERSISTED_QUERY_NOT_FOUND);
|
||||
constructor(customException: CustomException);
|
||||
|
||||
constructor(message?: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException?: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(
|
||||
messageOrException || 'PersistedQueryNotFound',
|
||||
ErrorCode.PERSISTED_QUERY_NOT_FOUND,
|
||||
extensions,
|
||||
);
|
||||
Object.defineProperty(this, 'name', {
|
||||
value: 'PersistedQueryNotFoundError',
|
||||
});
|
||||
@ -142,12 +191,15 @@ export class PersistedQueryNotFoundError extends BaseGraphQLError {
|
||||
}
|
||||
|
||||
export class PersistedQueryNotSupportedError extends BaseGraphQLError {
|
||||
constructor() {
|
||||
constructor(
|
||||
messageOrException?: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(
|
||||
'PersistedQueryNotSupported',
|
||||
messageOrException || 'PersistedQueryNotSupported',
|
||||
ErrorCode.PERSISTED_QUERY_NOT_SUPPORTED,
|
||||
extensions,
|
||||
);
|
||||
|
||||
Object.defineProperty(this, 'name', {
|
||||
value: 'PersistedQueryNotSupportedError',
|
||||
});
|
||||
@ -155,52 +207,71 @@ export class PersistedQueryNotSupportedError extends BaseGraphQLError {
|
||||
}
|
||||
|
||||
export class UserInputError extends BaseGraphQLError {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions) {
|
||||
super(message, ErrorCode.BAD_USER_INPUT, extensions);
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.BAD_USER_INPUT, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'UserInputError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError extends BaseGraphQLError {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions) {
|
||||
super(message, ErrorCode.NOT_FOUND, extensions);
|
||||
|
||||
Object.defineProperty(this, 'name', { value: 'NotFoundError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class MethodNotAllowedError extends BaseGraphQLError {
|
||||
constructor(message: string) {
|
||||
super(message, ErrorCode.METHOD_NOT_ALLOWED);
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.METHOD_NOT_ALLOWED, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'MethodNotAllowedError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class ConflictError extends BaseGraphQLError {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions) {
|
||||
super(message, ErrorCode.CONFLICT, extensions);
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.CONFLICT, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'ConflictError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class TimeoutError extends BaseGraphQLError {
|
||||
constructor(message: string) {
|
||||
super(message, ErrorCode.TIMEOUT);
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.TIMEOUT, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'TimeoutError' });
|
||||
}
|
||||
}
|
||||
|
||||
export class InternalServerError extends BaseGraphQLError {
|
||||
constructor(message: string) {
|
||||
super(message, ErrorCode.INTERNAL_SERVER_ERROR);
|
||||
constructor(exception: CustomException);
|
||||
|
||||
constructor(message: string, extensions?: RestrictedGraphQLErrorExtensions);
|
||||
|
||||
constructor(
|
||||
messageOrException: string | CustomException,
|
||||
extensions?: RestrictedGraphQLErrorExtensions,
|
||||
) {
|
||||
super(messageOrException, ErrorCode.INTERNAL_SERVER_ERROR, extensions);
|
||||
Object.defineProperty(this, 'name', { value: 'InternalServerError' });
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,9 +17,7 @@ export const recordTransformerGraphqlApiExceptionHandler = (
|
||||
case RecordTransformerExceptionCode.CONFLICTING_PHONE_CALLING_CODE_AND_COUNTRY_CODE:
|
||||
case RecordTransformerExceptionCode.INVALID_PHONE_CALLING_CODE:
|
||||
case RecordTransformerExceptionCode.INVALID_URL:
|
||||
throw new UserInputError(error.message, {
|
||||
userFriendlyMessage: error.userFriendlyMessage,
|
||||
});
|
||||
throw new UserInputError(error);
|
||||
default: {
|
||||
assertUnreachable(error.code);
|
||||
}
|
||||
|
||||
@ -19,13 +19,9 @@ export const handleWorkflowTriggerException = (
|
||||
case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER:
|
||||
case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_STATUS:
|
||||
case WorkflowTriggerExceptionCode.FORBIDDEN:
|
||||
throw new UserInputError(exception.message, {
|
||||
userFriendlyMessage: exception.userFriendlyMessage,
|
||||
});
|
||||
throw new UserInputError(exception);
|
||||
case WorkflowTriggerExceptionCode.NOT_FOUND:
|
||||
throw new NotFoundError(exception.message, {
|
||||
userFriendlyMessage: exception.userFriendlyMessage,
|
||||
});
|
||||
throw new NotFoundError(exception);
|
||||
case WorkflowTriggerExceptionCode.INTERNAL_ERROR:
|
||||
throw exception;
|
||||
default: {
|
||||
|
||||
@ -13,13 +13,13 @@ export const workspaceGraphqlApiExceptionHandler = (error: Error) => {
|
||||
switch (error.code) {
|
||||
case WorkspaceExceptionCode.SUBDOMAIN_NOT_FOUND:
|
||||
case WorkspaceExceptionCode.WORKSPACE_NOT_FOUND:
|
||||
throw new NotFoundError(error.message);
|
||||
throw new NotFoundError(error);
|
||||
case WorkspaceExceptionCode.DOMAIN_ALREADY_TAKEN:
|
||||
case WorkspaceExceptionCode.SUBDOMAIN_ALREADY_TAKEN:
|
||||
throw new ConflictError(error.message);
|
||||
throw new ConflictError(error);
|
||||
case WorkspaceExceptionCode.ENVIRONMENT_VAR_NOT_ENABLED:
|
||||
case WorkspaceExceptionCode.WORKSPACE_CUSTOM_DOMAIN_DISABLED:
|
||||
throw new ForbiddenError(error.message);
|
||||
throw new ForbiddenError(error);
|
||||
default: {
|
||||
const _exhaustiveCheck: never = error.code;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user