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

import {
  ForbiddenError,
  InternalServerError,
  NotFoundError,
  UserInputError,
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import {
  ChartDataException,
  ChartDataExceptionCode,
} from 'src/modules/dashboard/chart-data/exceptions/chart-data.exception';

export const chartDataGraphqlApiExceptionHandler = (error: Error) => {
  if (error instanceof ChartDataException) {
    switch (error.code) {
      case ChartDataExceptionCode.WIDGET_NOT_FOUND:
      case ChartDataExceptionCode.OBJECT_METADATA_NOT_FOUND:
      case ChartDataExceptionCode.FIELD_METADATA_NOT_FOUND:
        throw new NotFoundError(error.message);
      case ChartDataExceptionCode.INVALID_WIDGET_CONFIGURATION:
        throw new UserInputError(error.message);
      case ChartDataExceptionCode.PERMISSION_DENIED:
        throw new ForbiddenError(error.message);
      case ChartDataExceptionCode.QUERY_EXECUTION_FAILED:
      case ChartDataExceptionCode.TRANSFORMATION_FAILED:
        throw new InternalServerError(error.message);
      default: {
        return assertUnreachable(error.code);
      }
    }
  }

  throw error;
};