Skip to content

Commit 01a09dc

Browse files
committed
🔥 Improve generateError
1 parent dd3c02a commit 01a09dc

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/util/error.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,32 @@ import {
88
export const generateError = (
99
error: Error | AxiosError,
1010
intentMessage: string
11-
): ServerError => {
11+
) => {
1212
if (error instanceof AxiosError) {
1313
const message = error.response?.data
1414
? JSON.stringify(error.response?.data)
1515
: error.message;
1616
const status = error.response?.status || 500;
17+
var errorType: IntegrationErrorType;
1718
switch (status) {
1819
case 401:
19-
return new ServerError(
20-
DELEGATE_TO_FRONTEND_CODE,
21-
IntegrationErrorType.INTEGRATION_REFRESH_ERROR
22-
);
20+
errorType = IntegrationErrorType.INTEGRATION_REFRESH_ERROR;
21+
break;
2322
case 403:
24-
return new ServerError(
25-
DELEGATE_TO_FRONTEND_CODE,
26-
IntegrationErrorType.INTEGRATION_ERROR_FORBIDDEN
27-
);
23+
errorType = IntegrationErrorType.INTEGRATION_ERROR_FORBIDDEN;
24+
break;
2825
case 409:
29-
return new ServerError(
30-
DELEGATE_TO_FRONTEND_CODE,
31-
IntegrationErrorType.ENTITY_ERROR_CONFLICT
32-
);
26+
errorType = IntegrationErrorType.ENTITY_ERROR_CONFLICT;
27+
break;
3328
case 502:
3429
case 503:
3530
case 504:
36-
return new ServerError(
37-
DELEGATE_TO_FRONTEND_CODE,
38-
IntegrationErrorType.INTEGRATION_ERROR_UNAVAILABLE
39-
);
40-
31+
errorType = IntegrationErrorType.INTEGRATION_ERROR_UNAVAILABLE;
32+
break;
4133
default:
42-
return new ServerError(status, `${intentMessage} (${message})`);
34+
throw new ServerError(status, `${intentMessage} (${message})`);
4335
}
36+
throw new ServerError(DELEGATE_TO_FRONTEND_CODE, errorType);
4437
}
45-
return new ServerError(500, "An internal error occurred");
38+
throw new ServerError(500, "An internal error occurred");
4639
};

0 commit comments

Comments
 (0)