update HTTPService to throw APIError when server sends non 2xx response
This commit is contained in:
parent
f4ab5f42a0
commit
f1504272e6
|
@ -35,9 +35,14 @@ class HTTPService {
|
|||
xRequestId: response.headers['x-request-id'],
|
||||
httpStatusCode: response.status,
|
||||
errMessage: apiError.message,
|
||||
errCode: apiError.code,
|
||||
errCode: apiError.errCode,
|
||||
});
|
||||
return Promise.reject(response.data);
|
||||
throw new ApiError(
|
||||
apiError.message,
|
||||
apiError.errCode,
|
||||
response.status,
|
||||
response.statusText
|
||||
);
|
||||
} else {
|
||||
logError(error, 'HTTP Service Error', {
|
||||
url: config.url,
|
||||
|
@ -46,7 +51,21 @@ class HTTPService {
|
|||
xRequestId: response.headers['x-request-id'],
|
||||
status: response.status,
|
||||
});
|
||||
return Promise.reject(response);
|
||||
if (response.status >= 400 && response.status < 500) {
|
||||
throw new ApiError(
|
||||
response.statusText,
|
||||
'Client Error',
|
||||
response.status,
|
||||
response.statusText
|
||||
);
|
||||
} else {
|
||||
throw new ApiError(
|
||||
response.statusText,
|
||||
'Server Error',
|
||||
response.status,
|
||||
response.statusText
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if (error.request) {
|
||||
// The request was made but no response was received
|
||||
|
|
|
@ -1,6 +1,20 @@
|
|||
export interface ApiError {
|
||||
code: string;
|
||||
message: string;
|
||||
export class ApiError extends Error {
|
||||
httpStatus: number;
|
||||
httpStatusText: string;
|
||||
errCode: string;
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
errCode: string,
|
||||
httpStatus: number,
|
||||
httpStatusText: string
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'ApiError';
|
||||
this.errCode = errCode;
|
||||
this.httpStatus = httpStatus;
|
||||
this.httpStatusText = httpStatusText;
|
||||
}
|
||||
}
|
||||
|
||||
export function isApiError(object: any): object is ApiError {
|
||||
|
|
Loading…
Reference in a new issue