Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

throw custom ApiError when server sends custom message and code for error

Abhinavf4ab5f42a0e5
diff --git a/apps/photos/src/services/HTTPService.ts b/apps/photos/src/services/HTTPService.ts
index d60c100d40..a2c9d5a460 100644
--- a/apps/photos/src/services/HTTPService.ts
+++ b/apps/photos/src/services/HTTPService.ts
@@ -1,6 +1,7 @@
-import axios, { AxiosRequestConfig } from 'axios';
+import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { addLogLine } from 'utils/logging';
import { logError } from 'utils/sentry';
+import { ApiError, isApiError } from 'utils/error';
interface IHTTPHeaders {
[headerKey: string]: any;
@@ -18,34 +19,51 @@ class HTTPService {
axios.interceptors.response.use(
(response) => Promise.resolve(response),
(error) => {
+ const config = error.config as AxiosRequestConfig;
if (error.response) {
+ const response = error.response as AxiosResponse<
+ ApiError | any
+ >;
+
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
- logError(error, 'HTTP Service Error', {
- url: error.config.url,
- method: error.config.method,
- cfRay: error.response?.headers['cf-ray'],
- xRequestId: error.response?.headers['x-request-id'],
- status: error.response.status,
- });
- const { response } = error;
- return Promise.reject(response);
+ if (isApiError(response.data)) {
+ const apiError = response.data;
+ logError(error, 'HTTP Service Error', {
+ url: config.url,
+ method: config.method,
+ xRequestId: response.headers['x-request-id'],
+ httpStatusCode: response.status,
+ errMessage: apiError.message,
+ errCode: apiError.code,
+ });
+ return Promise.reject(response.data);
+ } else {
+ logError(error, 'HTTP Service Error', {
+ url: config.url,
+ method: config.method,
+ cfRay: response.headers['cf-ray'],
+ xRequestId: response.headers['x-request-id'],
+ status: response.status,
+ });
+ return Promise.reject(response);
+ }
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
addLogLine(
'request failed- no response',
- `url: ${error.config.url}`,
- `method: ${error.config.method}`
+ `url: ${config.url}`,
+ `method: ${config.method}`
);
return Promise.reject(error);
} else {
// Something happened in setting up the request that triggered an Error
addLogLine(
'request failed- axios error',
- `url: ${error.config.url}`,
- `method: ${error.config.method}`
+ `url: ${config.url}`,
+ `method: ${config.method}`
);
return Promise.reject(error);
}
diff --git a/apps/photos/src/utils/error/index.ts b/apps/photos/src/utils/error/index.ts
index 926518f1d9..64513d464f 100644
--- a/apps/photos/src/utils/error/index.ts
+++ b/apps/photos/src/utils/error/index.ts
@@ -1,3 +1,12 @@
+export interface ApiError {
+ code: string;
+ message: string;
+}
+
+export function isApiError(object: any): object is ApiError {
+ return 'code' in object && 'message' in object;
+}
+
export const ServerErrorCodes = {
SESSION_EXPIRED: '401',
NO_ACTIVE_SUBSCRIPTION: '402',