File size: 1,765 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
import { type MessageDescriptor } from '@lingui/core';
import { msg } from '@lingui/core/macro';
import { assertUnreachable } from 'twenty-shared/utils';

import { CustomException } from 'src/utils/custom-exception';

export enum FrontComponentExceptionCode {
  FRONT_COMPONENT_NOT_FOUND = 'FRONT_COMPONENT_NOT_FOUND',
  FRONT_COMPONENT_ALREADY_EXISTS = 'FRONT_COMPONENT_ALREADY_EXISTS',
  INVALID_FRONT_COMPONENT_INPUT = 'INVALID_FRONT_COMPONENT_INPUT',
  FRONT_COMPONENT_CREATE_FAILED = 'FRONT_COMPONENT_CREATE_FAILED',
  FRONT_COMPONENT_NOT_READY = 'FRONT_COMPONENT_NOT_READY',
}

const getFrontComponentExceptionUserFriendlyMessage = (
  code: FrontComponentExceptionCode,
) => {
  switch (code) {
    case FrontComponentExceptionCode.FRONT_COMPONENT_NOT_FOUND:
      return msg`Front component not found.`;
    case FrontComponentExceptionCode.FRONT_COMPONENT_ALREADY_EXISTS:
      return msg`A front component with this name already exists.`;
    case FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT:
      return msg`Invalid front component input.`;
    case FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED:
      return msg`Failed to create front component.`;
    case FrontComponentExceptionCode.FRONT_COMPONENT_NOT_READY:
      return msg`Front component is not ready.`;
    default:
      assertUnreachable(code);
  }
};

export class FrontComponentException extends CustomException<FrontComponentExceptionCode> {
  constructor(
    message: string,
    code: FrontComponentExceptionCode,
    { userFriendlyMessage }: { userFriendlyMessage?: MessageDescriptor } = {},
  ) {
    super(message, code, {
      userFriendlyMessage:
        userFriendlyMessage ??
        getFrontComponentExceptionUserFriendlyMessage(code),
    });
  }
}