Skip to content

Commit 334c153

Browse files
committed
🥅 Introduce new IntegrationErrorTypes and delegate them to the frontend
1 parent e1f8391 commit 334c153

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/models/integration-error.model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
export enum IntegrationErrorType {
22
INTEGRATION_INVALID_URL = "integration/invalid-url",
3+
4+
// TODO: rename to INTEGRATION_ERROR_REFRESH = "integration/error/refresh"
35
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+
411
CONTACT_CREATE_ERROR_CONFLICT = "contact/create-error/conflict",
512
CONTACT_CREATE_ERROR_EMAIL_CONFLICT = "contact/create-error/email-conflict",
613
CONTACT_ERROR_TOO_MANY_NUMBERS = "contact/error/too-many-numbers",

src/util/error.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
};

src/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./phone-number-utils";
22
export * from "./anonymize-key";
33
export * from "./logger.util";
44
export * from "./call-comment";
5+
export * from "./error";

0 commit comments

Comments
 (0)