diff options
author | crupest <crupest@outlook.com> | 2021-02-13 11:58:36 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-02-13 11:58:36 +0800 |
commit | 6159c8fd8e4d9ded22b9d5dd1772b1529a1d513a (patch) | |
tree | 7ae2fd68896dcd426dd6bb5183680ac226f468cf /FrontEnd/src/app/http/common.ts | |
parent | 99d525560f6aefa5f5fb63e3ba2fff3b557d61f6 (diff) | |
download | timeline-6159c8fd8e4d9ded22b9d5dd1772b1529a1d513a.tar.gz timeline-6159c8fd8e4d9ded22b9d5dd1772b1529a1d513a.tar.bz2 timeline-6159c8fd8e4d9ded22b9d5dd1772b1529a1d513a.zip |
...
Diffstat (limited to 'FrontEnd/src/app/http/common.ts')
-rw-r--r-- | FrontEnd/src/app/http/common.ts | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/FrontEnd/src/app/http/common.ts b/FrontEnd/src/app/http/common.ts index 0f46280c..5c44e8e3 100644 --- a/FrontEnd/src/app/http/common.ts +++ b/FrontEnd/src/app/http/common.ts @@ -4,6 +4,45 @@ export const apiBaseUrl = "/api"; export const axios = rawAxios.create(); +function convertToNetworkError(error: AxiosError): never { + if (error.isAxiosError && error.response == null) { + throw new HttpNetworkError(error); + } else { + throw error; + } +} + +function convertToForbiddenError(error: AxiosError): never { + if ( + error.isAxiosError && + error.response != null && + (error.response.status == 401 || error.response.status == 403) + ) { + throw new HttpForbiddenError(error); + } else { + throw error; + } +} + +function convertToNotFoundError(error: AxiosError): never { + if ( + error.isAxiosError && + error.response != null && + error.response.status == 404 + ) { + throw new HttpNotFoundError(error); + } else { + throw error; + } +} + +rawAxios.interceptors.response.use(undefined, convertToNetworkError); +rawAxios.interceptors.response.use(undefined, convertToForbiddenError); +rawAxios.interceptors.response.use(undefined, convertToNotFoundError); +axios.interceptors.response.use(undefined, convertToNetworkError); +axios.interceptors.response.use(undefined, convertToForbiddenError); +axios.interceptors.response.use(undefined, convertToNotFoundError); + let _token: string | null = null; export function getHttpToken(): string | null { @@ -71,6 +110,12 @@ export class HttpForbiddenError extends Error { } } +export class HttpNotFoundError extends Error { + constructor(public innerError?: AxiosError) { + super(); + } +} + export class NotModified {} export interface BlobWithEtag { @@ -135,30 +180,6 @@ export function convertToIfErrorCodeIs<NewError>( }); } -export function convertToNetworkError( - error: AxiosError<CommonErrorResponse> -): never { - if (error.isAxiosError && error.response == null) { - throw new HttpNetworkError(error); - } else { - throw error; - } -} - -export function convertToForbiddenError( - error: AxiosError<CommonErrorResponse> -): never { - if ( - error.isAxiosError && - error.response != null && - (error.response.status == 401 || error.response.status == 403) - ) { - throw new HttpForbiddenError(error); - } else { - throw error; - } -} - export function convertToNotModified( error: AxiosError<CommonErrorResponse> ): NotModified { |