File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1
1
export enum IntegrationErrorType {
2
2
INTEGRATION_INVALID_URL = "integration/invalid-url" ,
3
+
4
+ // TODO: rename to INTEGRATION_ERROR_REFRESH = "integration/error/refresh"
3
5
INTEGRATION_REFRESH_ERROR = "integration/refresh-error" ,
6
+ INTEGRATION_ERROR_FORBIDDEN = "integration/error/forbidden" ,
7
+ INTEGRATION_ERROR_UNAVAILABLE = "integration/error/unavailable" ,
8
+
9
+ ENTITY_ERROR_CONFLICT = "entity/error/conflict" ,
10
+
4
11
CONTACT_CREATE_ERROR_CONFLICT = "contact/create-error/conflict" ,
5
12
CONTACT_CREATE_ERROR_EMAIL_CONFLICT = "contact/create-error/email-conflict" ,
6
13
CONTACT_ERROR_TOO_MANY_NUMBERS = "contact/error/too-many-numbers" ,
Original file line number Diff line number Diff line change
1
+ import { AxiosError } from "axios" ;
2
+ import {
3
+ DELEGATE_TO_FRONTEND_CODE ,
4
+ IntegrationErrorType ,
5
+ ServerError ,
6
+ } from "../models" ;
7
+
8
+ export const generateError = (
9
+ error : Error | AxiosError ,
10
+ intentMessage : string
11
+ ) : ServerError => {
12
+ if ( error instanceof AxiosError ) {
13
+ const message = error . response ?. data
14
+ ? JSON . stringify ( error . response ?. data )
15
+ : error . message ;
16
+ const status = error . response ?. status || 500 ;
17
+ switch ( status ) {
18
+ case 401 :
19
+ return new ServerError (
20
+ DELEGATE_TO_FRONTEND_CODE ,
21
+ IntegrationErrorType . INTEGRATION_REFRESH_ERROR
22
+ ) ;
23
+ case 403 :
24
+ return new ServerError (
25
+ DELEGATE_TO_FRONTEND_CODE ,
26
+ IntegrationErrorType . INTEGRATION_ERROR_FORBIDDEN
27
+ ) ;
28
+ case 409 :
29
+ return new ServerError (
30
+ DELEGATE_TO_FRONTEND_CODE ,
31
+ IntegrationErrorType . ENTITY_ERROR_CONFLICT
32
+ ) ;
33
+ case 502 :
34
+ case 503 :
35
+ case 504 :
36
+ return new ServerError (
37
+ DELEGATE_TO_FRONTEND_CODE ,
38
+ IntegrationErrorType . INTEGRATION_ERROR_UNAVAILABLE
39
+ ) ;
40
+
41
+ default :
42
+ return new ServerError ( status , `${ intentMessage } (${ message } )` ) ;
43
+ }
44
+ }
45
+ return new ServerError ( 500 , "An internal error occurred" ) ;
46
+ } ;
Original file line number Diff line number Diff line change @@ -2,3 +2,4 @@ export * from "./phone-number-utils";
2
2
export * from "./anonymize-key" ;
3
3
export * from "./logger.util" ;
4
4
export * from "./call-comment" ;
5
+ export * from "./error" ;
You can’t perform that action at this time.
0 commit comments