7
7
import { errorLogger } from "./logger.util" ;
8
8
9
9
export const throwAndDelegateError = (
10
- error : AxiosError | ServerError | Error ,
10
+ error : AxiosError | DelegateToFrontedError | ServerError | Error ,
11
11
source : string ,
12
12
apiKey : string | undefined ,
13
13
logMessage ?: string
@@ -31,28 +31,35 @@ export const throwAndDelegateError = (
31
31
err . response ?. status ||
32
32
( err . code ? parseInt ( err . code ) : 500 ) ;
33
33
34
- var errorType : IntegrationErrorType ;
35
- switch ( status ) {
36
- case 401 :
37
- errorType = IntegrationErrorType . INTEGRATION_REFRESH_ERROR ;
38
- break ;
39
- case 403 :
40
- errorType = IntegrationErrorType . INTEGRATION_ERROR_FORBIDDEN ;
41
- break ;
42
- case 404 :
43
- errorType = IntegrationErrorType . ENTITY_NOT_FOUND ;
44
- break ;
45
- case 409 :
46
- errorType = IntegrationErrorType . ENTITY_ERROR_CONFLICT ;
47
- break ;
48
- case 502 :
49
- case 503 :
50
- case 504 :
51
- errorType = IntegrationErrorType . INTEGRATION_ERROR_UNAVAILABLE ;
52
- break ;
53
- default :
54
- throw new ServerError ( status , `${ source } (${ errorMessage } )` ) ;
34
+ var errorType : IntegrationErrorType | string ;
35
+
36
+ if ( error instanceof DelegateToFrontedError ) {
37
+ var delegateToFrontedError = error as DelegateToFrontedError ;
38
+ errorType = delegateToFrontedError . errorType ;
39
+ } else {
40
+ switch ( status ) {
41
+ case 401 :
42
+ errorType = IntegrationErrorType . INTEGRATION_REFRESH_ERROR ;
43
+ break ;
44
+ case 403 :
45
+ errorType = IntegrationErrorType . INTEGRATION_ERROR_FORBIDDEN ;
46
+ break ;
47
+ case 404 :
48
+ errorType = IntegrationErrorType . ENTITY_NOT_FOUND ;
49
+ break ;
50
+ case 409 :
51
+ errorType = IntegrationErrorType . ENTITY_ERROR_CONFLICT ;
52
+ break ;
53
+ case 502 :
54
+ case 503 :
55
+ case 504 :
56
+ errorType = IntegrationErrorType . INTEGRATION_ERROR_UNAVAILABLE ;
57
+ break ;
58
+ default :
59
+ throw new ServerError ( status , `${ source } (${ errorMessage } )` ) ;
60
+ }
55
61
}
62
+
56
63
errorLogger (
57
64
"throwAndDelegateError" ,
58
65
`Delegating error to frontend with code ${ DELEGATE_TO_FRONTEND_CODE } and type ${ errorType } ` ,
@@ -62,3 +69,13 @@ export const throwAndDelegateError = (
62
69
}
63
70
throw new ServerError ( 500 , "An internal error occurred" ) ;
64
71
} ;
72
+
73
+ export class DelegateToFrontedError extends Error {
74
+ errorType : IntegrationErrorType ;
75
+ constructor ( errorType : IntegrationErrorType ) {
76
+ super ( errorType ) ;
77
+
78
+ this . errorType = errorType ;
79
+ this . name = "DelegateToFrontedError" ;
80
+ }
81
+ }
0 commit comments