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

import { STANDARD_ERROR_MESSAGE } from 'src/engine/api/common/common-query-runners/errors/standard-error-message.constant';
import { CustomException } from 'src/utils/custom-exception';

export enum RelationExceptionCode {
  RELATION_OBJECT_METADATA_NOT_FOUND = 'RELATION_OBJECT_METADATA_NOT_FOUND',
  RELATION_TARGET_FIELD_METADATA_ID_NOT_FOUND = 'RELATION_TARGET_FIELD_METADATA_ID_NOT_FOUND',
  RELATION_JOIN_COLUMN_ON_BOTH_SIDES = 'RELATION_JOIN_COLUMN_ON_BOTH_SIDES',
  MISSING_RELATION_JOIN_COLUMN = 'MISSING_RELATION_JOIN_COLUMN',
  MULTIPLE_JOIN_COLUMNS_FOUND = 'MULTIPLE_JOIN_COLUMNS_FOUND',
}

const getRelationExceptionUserFriendlyMessage = (
  code: RelationExceptionCode,
) => {
  switch (code) {
    case RelationExceptionCode.RELATION_OBJECT_METADATA_NOT_FOUND:
    case RelationExceptionCode.RELATION_TARGET_FIELD_METADATA_ID_NOT_FOUND:
    case RelationExceptionCode.RELATION_JOIN_COLUMN_ON_BOTH_SIDES:
    case RelationExceptionCode.MISSING_RELATION_JOIN_COLUMN:
    case RelationExceptionCode.MULTIPLE_JOIN_COLUMNS_FOUND:
      return STANDARD_ERROR_MESSAGE;
    default:
      assertUnreachable(code);
  }
};

export class RelationException extends CustomException<RelationExceptionCode> {
  constructor(
    message: string,
    code: RelationExceptionCode,
    { userFriendlyMessage }: { userFriendlyMessage?: MessageDescriptor } = {},
  ) {
    super(message, code, {
      userFriendlyMessage:
        userFriendlyMessage ?? getRelationExceptionUserFriendlyMessage(code),
    });
  }
}