File size: 1,131 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
import { assertUnreachable } from 'twenty-shared/utils';

import {
  ConflictError,
  ForbiddenError,
  NotFoundError,
  UserInputError,
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import {
  FrontComponentException,
  FrontComponentExceptionCode,
} from 'src/engine/metadata-modules/front-component/front-component.exception';

export const frontComponentGraphqlApiExceptionHandler = (error: Error) => {
  if (error instanceof FrontComponentException) {
    switch (error.code) {
      case FrontComponentExceptionCode.FRONT_COMPONENT_NOT_FOUND:
        throw new NotFoundError(error);
      case FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT:
        throw new UserInputError(error);
      case FrontComponentExceptionCode.FRONT_COMPONENT_ALREADY_EXISTS:
        throw new ConflictError(error);
      case FrontComponentExceptionCode.FRONT_COMPONENT_NOT_READY:
        throw new ForbiddenError(error);
      case FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED:
        throw error;
      default: {
        return assertUnreachable(error.code);
      }
    }
  }

  throw error;
};