Skip to content

Commit 5c8e193

Browse files
committed
✨ Add DelegateToFrontedError class
1 parent 7079a60 commit 5c8e193

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

src/util/error.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { errorLogger } from "./logger.util";
88

99
export const throwAndDelegateError = (
10-
error: AxiosError | ServerError | Error,
10+
error: AxiosError | DelegateToFrontedError | ServerError | Error,
1111
source: string,
1212
apiKey: string | undefined,
1313
logMessage?: string
@@ -31,28 +31,35 @@ export const throwAndDelegateError = (
3131
err.response?.status ||
3232
(err.code ? parseInt(err.code) : 500);
3333

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+
}
5561
}
62+
5663
errorLogger(
5764
"throwAndDelegateError",
5865
`Delegating error to frontend with code ${DELEGATE_TO_FRONTEND_CODE} and type ${errorType}`,
@@ -62,3 +69,13 @@ export const throwAndDelegateError = (
6269
}
6370
throw new ServerError(500, "An internal error occurred");
6471
};
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

Comments
 (0)