|
1 |
| -import { GoogleCalendarError } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/types/google-calendar-error.type'; |
2 | 1 | import {
|
3 |
| - CalendarEventError, |
4 |
| - CalendarEventErrorCode, |
5 |
| -} from 'src/modules/calendar/calendar-event-import-manager/types/calendar-event-error.type'; |
| 2 | + CalendarEventImportDriverException, |
| 3 | + CalendarEventImportDriverExceptionCode, |
| 4 | +} from 'src/modules/calendar/calendar-event-import-manager/drivers/exceptions/calendar-event-import-driver.exception'; |
6 | 5 |
|
7 |
| -export const parseGoogleCalendarError = ( |
8 |
| - error: GoogleCalendarError, |
9 |
| -): CalendarEventError => { |
| 6 | +export const parseGoogleCalendarError = (error: { |
| 7 | + code?: number; |
| 8 | + reason: string; |
| 9 | + message: string; |
| 10 | +}): CalendarEventImportDriverException => { |
10 | 11 | const { code, reason, message } = error;
|
11 | 12 |
|
12 | 13 | switch (code) {
|
13 | 14 | case 400:
|
14 | 15 | if (reason === 'invalid_grant') {
|
15 |
| - return { |
16 |
| - code: CalendarEventErrorCode.INSUFFICIENT_PERMISSIONS, |
| 16 | + return new CalendarEventImportDriverException( |
17 | 17 | message,
|
18 |
| - }; |
| 18 | + CalendarEventImportDriverExceptionCode.INSUFFICIENT_PERMISSIONS, |
| 19 | + ); |
19 | 20 | }
|
20 | 21 | if (reason === 'failedPrecondition') {
|
21 |
| - return { |
22 |
| - code: CalendarEventErrorCode.TEMPORARY_ERROR, |
| 22 | + return new CalendarEventImportDriverException( |
23 | 23 | message,
|
24 |
| - }; |
| 24 | + CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR, |
| 25 | + ); |
25 | 26 | }
|
26 | 27 |
|
27 |
| - return { |
28 |
| - code: CalendarEventErrorCode.UNKNOWN, |
| 28 | + return new CalendarEventImportDriverException( |
29 | 29 | message,
|
30 |
| - }; |
| 30 | + CalendarEventImportDriverExceptionCode.UNKNOWN, |
| 31 | + ); |
31 | 32 |
|
32 | 33 | case 404:
|
33 |
| - return { |
34 |
| - code: CalendarEventErrorCode.NOT_FOUND, |
| 34 | + return new CalendarEventImportDriverException( |
35 | 35 | message,
|
36 |
| - }; |
| 36 | + CalendarEventImportDriverExceptionCode.NOT_FOUND, |
| 37 | + ); |
37 | 38 |
|
38 | 39 | case 429:
|
39 |
| - return { |
40 |
| - code: CalendarEventErrorCode.TEMPORARY_ERROR, |
| 40 | + return new CalendarEventImportDriverException( |
41 | 41 | message,
|
42 |
| - }; |
| 42 | + CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR, |
| 43 | + ); |
43 | 44 |
|
44 | 45 | case 403:
|
45 | 46 | if (
|
46 | 47 | reason === 'rateLimitExceeded' ||
|
47 | 48 | reason === 'userRateLimitExceeded'
|
48 | 49 | ) {
|
49 |
| - return { |
50 |
| - code: CalendarEventErrorCode.TEMPORARY_ERROR, |
| 50 | + return new CalendarEventImportDriverException( |
51 | 51 | message,
|
52 |
| - }; |
| 52 | + CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR, |
| 53 | + ); |
53 | 54 | } else {
|
54 |
| - return { |
55 |
| - code: CalendarEventErrorCode.INSUFFICIENT_PERMISSIONS, |
| 55 | + return new CalendarEventImportDriverException( |
56 | 56 | message,
|
57 |
| - }; |
| 57 | + CalendarEventImportDriverExceptionCode.INSUFFICIENT_PERMISSIONS, |
| 58 | + ); |
58 | 59 | }
|
59 | 60 |
|
60 | 61 | case 401:
|
61 |
| - return { |
62 |
| - code: CalendarEventErrorCode.INSUFFICIENT_PERMISSIONS, |
| 62 | + return new CalendarEventImportDriverException( |
63 | 63 | message,
|
64 |
| - }; |
| 64 | + CalendarEventImportDriverExceptionCode.INSUFFICIENT_PERMISSIONS, |
| 65 | + ); |
65 | 66 | case 500:
|
66 | 67 | if (reason === 'backendError') {
|
67 |
| - return { |
68 |
| - code: CalendarEventErrorCode.TEMPORARY_ERROR, |
| 68 | + return new CalendarEventImportDriverException( |
69 | 69 | message,
|
70 |
| - }; |
| 70 | + CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR, |
| 71 | + ); |
71 | 72 | } else {
|
72 |
| - return { |
73 |
| - code: CalendarEventErrorCode.UNKNOWN, |
| 73 | + return new CalendarEventImportDriverException( |
74 | 74 | message,
|
75 |
| - }; |
| 75 | + CalendarEventImportDriverExceptionCode.UNKNOWN, |
| 76 | + ); |
76 | 77 | }
|
77 | 78 |
|
78 | 79 | default:
|
79 | 80 | break;
|
80 | 81 | }
|
81 | 82 |
|
82 |
| - return { |
83 |
| - code: CalendarEventErrorCode.UNKNOWN, |
| 83 | + return new CalendarEventImportDriverException( |
84 | 84 | message,
|
85 |
| - }; |
| 85 | + CalendarEventImportDriverExceptionCode.UNKNOWN, |
| 86 | + ); |
86 | 87 | };
|
0 commit comments