|
16 | 16 | * limitations under the License. |
17 | 17 | */ |
18 | 18 |
|
19 | | -class APIError extends Error { |
20 | | - case: string | undefined; |
21 | | - response: Response | undefined; |
22 | | - error: unknown; |
23 | | - errorData: unknown; |
24 | | - |
25 | | - constructor(arg: string | undefined = undefined) { |
26 | | - super(arg || "Renku API error"); |
27 | | - } |
28 | | -} |
29 | | - |
30 | | -const API_ERRORS = { |
| 19 | +export const API_ERRORS = { |
31 | 20 | unauthorizedError: "UNAUTHORIZED", |
32 | 21 | forbiddenError: "FORBIDDEN", |
33 | 22 | notFoundError: "NOT_FOUND", |
34 | 23 | internalServerError: "SERVER_ERROR", |
35 | 24 | networkError: "NETWORK_PROBLEM", |
36 | 25 | authExpired: "AUTH_EXPIRED", |
37 | 26 | }; |
38 | | - |
39 | | -function throwAuthError(response: Response) { |
40 | | - const error = new APIError(); |
41 | | - error.case = API_ERRORS.authExpired; |
42 | | - error.response = response; |
43 | | - return Promise.reject(error); |
44 | | -} |
45 | | - |
46 | | -function throwErrorWithData(response: Response, data: unknown) { |
47 | | - let error; |
48 | | - switch (response.status) { |
49 | | - case 401: |
50 | | - error = new APIError(); |
51 | | - error.case = API_ERRORS.unauthorizedError; |
52 | | - break; |
53 | | - case 403: |
54 | | - error = new APIError(); |
55 | | - error.case = API_ERRORS.forbiddenError; |
56 | | - break; |
57 | | - case 404: |
58 | | - error = new APIError(); |
59 | | - error.case = API_ERRORS.notFoundError; |
60 | | - break; |
61 | | - case 500: |
62 | | - error = new APIError(); |
63 | | - error.case = API_ERRORS.internalServerError; |
64 | | - break; |
65 | | - default: |
66 | | - error = new APIError(); |
67 | | - } |
68 | | - error.response = response; |
69 | | - error.errorData = data; |
70 | | - return Promise.reject(error); |
71 | | -} |
72 | | - |
73 | | -function throwAPIErrors(response: Response) { |
74 | | - const contentType = response.headers.get("Content-Type"); |
75 | | - // TODO The default should be to check for type application/json |
76 | | - // but I want to make a more minimal change to the code right now. |
77 | | - if (contentType === "text/html") |
78 | | - return response.text().then((d) => throwErrorWithData(response, d)); |
79 | | - |
80 | | - return response.json().then((d) => throwErrorWithData(response, d)); |
81 | | -} |
82 | | - |
83 | | -function alertAPIErrors(error: APIError) { |
84 | | - switch (error.case) { |
85 | | - case API_ERRORS.forbiddenError: |
86 | | - throw Error( |
87 | | - "You don't have the necessary permission to view this information or perform this action." |
88 | | - ); |
89 | | - case API_ERRORS.notFoundError: |
90 | | - throw Error("We could not find the requested resource on the server."); |
91 | | - case API_ERRORS.internalServerError: |
92 | | - throw Error( |
93 | | - "There is a problem with the server - please try again later." |
94 | | - ); |
95 | | - case API_ERRORS.networkError: |
96 | | - throw Error( |
97 | | - "There seems to be problem with your network connection. Please check and try again." |
98 | | - ); |
99 | | - default: |
100 | | - // No alert on default exception |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -export { APIError, API_ERRORS, alertAPIErrors, throwAPIErrors, throwAuthError }; |
0 commit comments