Files
twenty/packages/twenty-server/src/engine/metadata-modules/relation-metadata/utils/relation-metadata-graphql-api-exception-handler.util.ts
T
Thomas Trompette a15884ea0a Add exceptions for metadata modules (#6070)
Class exception for each metadata module + handler to map on graphql
error

TODO left :
- find a way to call handler on auto-resolvers nestjs query (probably
interceptors)
- discuss what should be done for pre-hooks errors
- discuss what should be done for Unauthorized exception
2024-07-01 13:49:17 +02:00

29 lines
951 B
TypeScript

import {
RelationMetadataException,
RelationMetadataExceptionCode,
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.exception';
import {
UserInputError,
ConflictError,
InternalServerError,
NotFoundError,
} from 'src/engine/utils/graphql-errors.util';
export const relationMetadataGraphqlApiExceptionHandler = (error: Error) => {
if (error instanceof RelationMetadataException) {
switch (error.code) {
case RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND:
throw new NotFoundError(error.message);
case RelationMetadataExceptionCode.INVALID_RELATION_INPUT:
throw new UserInputError(error.message);
case RelationMetadataExceptionCode.RELATION_ALREADY_EXISTS:
throw new ConflictError(error.message);
case RelationMetadataExceptionCode.FOREIGN_KEY_NOT_FOUND:
default:
throw new InternalServerError(error.message);
}
}
throw error;
};