File size: 3,723 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
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 LogicFunctionExceptionCode {
  LOGIC_FUNCTION_NOT_FOUND = 'LOGIC_FUNCTION_NOT_FOUND',
  LOGIC_FUNCTION_ALREADY_EXIST = 'LOGIC_FUNCTION_ALREADY_EXIST',
  LOGIC_FUNCTION_NOT_READY = 'LOGIC_FUNCTION_NOT_READY',
  LOGIC_FUNCTION_BUILDING = 'LOGIC_FUNCTION_BUILDING',
  LOGIC_FUNCTION_CODE_UNCHANGED = 'LOGIC_FUNCTION_CODE_UNCHANGED',
  LOGIC_FUNCTION_EXECUTION_LIMIT_REACHED = 'LOGIC_FUNCTION_EXECUTION_LIMIT_REACHED',
  LOGIC_FUNCTION_CREATE_FAILED = 'LOGIC_FUNCTION_CREATE_FAILED',
  LOGIC_FUNCTION_COMPILATION_FAILED = 'LOGIC_FUNCTION_COMPILATION_FAILED',
  LOGIC_FUNCTION_EXECUTION_TIMEOUT = 'LOGIC_FUNCTION_EXECUTION_TIMEOUT',
  LOGIC_FUNCTION_PLATFORM_EXECUTION_ERROR = 'LOGIC_FUNCTION_PLATFORM_EXECUTION_ERROR',
  LOGIC_FUNCTION_LAYER_BUILD_FAILED = 'LOGIC_FUNCTION_LAYER_BUILD_FAILED',
  LOGIC_FUNCTION_DISABLED = 'LOGIC_FUNCTION_DISABLED',
  LOGIC_FUNCTION_INVALID_SEED_PROJECT = 'LOGIC_FUNCTION_INVALID_SEED_PROJECT',
  INVALID_LOGIC_FUNCTION_INPUT = 'INVALID_LOGIC_FUNCTION_INPUT',
  LOGIC_FUNCTION_PREBUILT_BUNDLE_NOT_INSTALLED = 'LOGIC_FUNCTION_PREBUILT_BUNDLE_NOT_INSTALLED',
}

const getLogicFunctionExceptionUserFriendlyMessage = (
  code: LogicFunctionExceptionCode,
) => {
  switch (code) {
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_NOT_FOUND:
      return msg`Function not found.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_ALREADY_EXIST:
      return msg`A function with this name already exists.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_NOT_READY:
      return msg`Function is not ready.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_BUILDING:
      return msg`Function is currently building.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_CODE_UNCHANGED:
      return msg`Function code is unchanged.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_EXECUTION_LIMIT_REACHED:
      return msg`Function execution limit reached.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_CREATE_FAILED:
      return msg`Failed to create function.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_COMPILATION_FAILED:
      return msg`Function code failed to compile.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_EXECUTION_TIMEOUT:
      return msg`Function execution timed out.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_PLATFORM_EXECUTION_ERROR:
      return msg`Function execution failed.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_LAYER_BUILD_FAILED:
      return msg`Failed to build function dependencies.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_DISABLED:
      return msg`Logic function execution is disabled.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_INVALID_SEED_PROJECT:
      return msg`Invalid seed project configuration.`;
    case LogicFunctionExceptionCode.INVALID_LOGIC_FUNCTION_INPUT:
      return msg`Invalid logic function input.`;
    case LogicFunctionExceptionCode.LOGIC_FUNCTION_PREBUILT_BUNDLE_NOT_INSTALLED:
      return msg`Prebuilt bundle is not installed on the function. Rebuild and try again.`;
    default:
      assertUnreachable(code);
  }
};

export class LogicFunctionException extends CustomException<LogicFunctionExceptionCode> {
  constructor(
    message: string,
    code: LogicFunctionExceptionCode,
    { userFriendlyMessage }: { userFriendlyMessage?: MessageDescriptor } = {},
  ) {
    super(message, code, {
      userFriendlyMessage:
        userFriendlyMessage ??
        getLogicFunctionExceptionUserFriendlyMessage(code),
    });
  }
}