2020-04-14 23:45:21 +08:00
|
|
|
/*
|
|
|
|
* @Author: early-autumn
|
|
|
|
* @Date: 2020-04-14 22:23:39
|
|
|
|
* @LastEditors: early-autumn
|
2020-05-02 15:47:38 +08:00
|
|
|
* @LastEditTime: 2020-04-25 09:23:40
|
2020-04-14 23:45:21 +08:00
|
|
|
*/
|
2020-04-18 17:06:01 +08:00
|
|
|
import { AxiosRequestConfig, RequestConfig, AxiosResponse } from '../types';
|
2020-04-14 23:45:21 +08:00
|
|
|
|
2020-04-16 00:23:24 +08:00
|
|
|
/**
|
|
|
|
* AxiosError 继承自 Error
|
|
|
|
*/
|
2020-04-14 23:45:21 +08:00
|
|
|
class AxiosError extends Error {
|
2020-04-16 00:23:24 +08:00
|
|
|
/**
|
|
|
|
* 是 Axios 错误
|
|
|
|
*/
|
2020-04-24 09:24:43 +08:00
|
|
|
public isAxiosError = true;
|
2020-04-14 23:45:21 +08:00
|
|
|
|
2020-04-23 10:46:45 +08:00
|
|
|
/**
|
|
|
|
* @param message 错误信息
|
|
|
|
* @param config Axios 请求配置
|
|
|
|
* @param request 通用请求配置
|
|
|
|
* @param response Axios 响应体
|
|
|
|
*/
|
2020-05-02 15:47:38 +08:00
|
|
|
public constructor(
|
2020-04-24 09:24:43 +08:00
|
|
|
message: string,
|
|
|
|
public config: AxiosRequestConfig,
|
|
|
|
public request: RequestConfig,
|
|
|
|
public response?: AxiosResponse
|
|
|
|
) {
|
2020-04-14 23:45:21 +08:00
|
|
|
super(message);
|
|
|
|
|
2020-04-16 00:23:24 +08:00
|
|
|
// 修复继承系统自带类 prototype 设置失败的问题
|
2020-04-14 23:45:21 +08:00
|
|
|
Object.setPrototypeOf(this, AxiosError.prototype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 00:23:24 +08:00
|
|
|
/**
|
|
|
|
* 创建 AxiosError 的工厂方法
|
|
|
|
*
|
|
|
|
* 返回一个新的 AxiosError 对象
|
|
|
|
*
|
|
|
|
* @param message 错误信息
|
2020-04-17 19:45:55 +08:00
|
|
|
* @param config Axios 请求配置
|
2020-04-18 17:06:01 +08:00
|
|
|
* @param request 通用请求配置
|
|
|
|
* @param response Axios 响应体
|
2020-04-16 00:23:24 +08:00
|
|
|
*/
|
2020-04-17 15:42:38 +08:00
|
|
|
export default function createError(
|
|
|
|
message: string,
|
|
|
|
config: AxiosRequestConfig,
|
2020-04-18 17:06:01 +08:00
|
|
|
request: RequestConfig,
|
2020-04-17 15:42:38 +08:00
|
|
|
response?: AxiosResponse
|
|
|
|
): AxiosError {
|
|
|
|
return new AxiosError(message, config, request, response);
|
2020-04-14 23:45:21 +08:00
|
|
|
}
|