2020-04-14 23:45:21 +08:00
|
|
|
/*
|
|
|
|
* @Author: early-autumn
|
|
|
|
* @Date: 2020-04-14 22:23:39
|
|
|
|
* @LastEditors: early-autumn
|
2020-04-16 00:23:24 +08:00
|
|
|
* @LastEditTime: 2020-04-15 15:50:18
|
2020-04-14 23:45:21 +08:00
|
|
|
*/
|
2020-04-16 00:23:24 +08:00
|
|
|
import { AxiosRequest, 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-14 23:45:21 +08:00
|
|
|
isAxiosError = true;
|
2020-04-16 00:23:24 +08:00
|
|
|
/**
|
|
|
|
* 请求配置
|
|
|
|
*/
|
|
|
|
config: AxiosRequest;
|
|
|
|
/**
|
|
|
|
* 请求响应体
|
|
|
|
*/
|
2020-04-14 23:45:21 +08:00
|
|
|
response?: AxiosResponse;
|
|
|
|
|
2020-04-16 00:23:24 +08:00
|
|
|
constructor(message: string, config: AxiosRequest, response?: AxiosResponse) {
|
2020-04-14 23:45:21 +08:00
|
|
|
super(message);
|
|
|
|
|
|
|
|
this.config = config;
|
|
|
|
this.response = response;
|
|
|
|
|
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 错误信息
|
|
|
|
* @param config 请求配置
|
|
|
|
* @param response 请求响应体
|
|
|
|
*/
|
|
|
|
export default function createError(message: string, config: AxiosRequest, response?: AxiosResponse): AxiosError {
|
2020-04-14 23:45:21 +08:00
|
|
|
return new AxiosError(message, config, response);
|
|
|
|
}
|