2023-04-02 18:26:24 +08:00
|
|
|
import { cleanStack } from '../helpers/error';
|
2023-03-24 20:15:51 +08:00
|
|
|
import { AxiosAdapterTask } from '../adapter';
|
2023-03-23 20:09:00 +08:00
|
|
|
import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
|
|
|
|
|
|
|
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
|
|
|
|
|
|
|
class AxiosError extends Error {
|
|
|
|
public config: AxiosRequestConfig;
|
2023-04-05 16:35:25 +08:00
|
|
|
public request: AxiosAdapterTask;
|
|
|
|
public response: AxiosErrorResponse;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
|
|
public constructor(
|
|
|
|
message: string,
|
|
|
|
config: AxiosRequestConfig,
|
2023-04-05 16:35:25 +08:00
|
|
|
response: AxiosErrorResponse,
|
|
|
|
request: AxiosAdapterTask,
|
2023-03-23 20:09:00 +08:00
|
|
|
) {
|
|
|
|
super(message);
|
|
|
|
|
|
|
|
this.config = config;
|
|
|
|
this.request = request;
|
|
|
|
this.response = response;
|
|
|
|
|
|
|
|
Object.setPrototypeOf(this, AxiosError.prototype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createError(
|
|
|
|
message: string,
|
|
|
|
config: AxiosRequestConfig,
|
2023-04-05 16:35:25 +08:00
|
|
|
response: AxiosErrorResponse,
|
|
|
|
request: AxiosAdapterTask,
|
2023-03-23 20:09:00 +08:00
|
|
|
): AxiosError {
|
2023-04-05 16:35:25 +08:00
|
|
|
const axiosError = new AxiosError(message, config, response, request);
|
2023-04-02 18:26:24 +08:00
|
|
|
cleanStack(axiosError);
|
|
|
|
return axiosError;
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
2023-04-05 13:31:48 +08:00
|
|
|
|
|
|
|
export function isAxiosError(value: unknown): value is AxiosError {
|
|
|
|
return value instanceof AxiosError;
|
|
|
|
}
|