2020-04-14 23:45:21 +08:00
|
|
|
/*
|
|
|
|
* @Author: early-autumn
|
|
|
|
* @Date: 2020-04-14 22:23:39
|
|
|
|
* @LastEditors: early-autumn
|
2020-04-17 12:06:41 +08:00
|
|
|
* @LastEditTime: 2020-04-17 09:48:30
|
2020-04-14 23:45:21 +08:00
|
|
|
*/
|
2020-04-17 00:18:59 +08:00
|
|
|
import { AxiosRequestConfig, 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-17 00:18:59 +08:00
|
|
|
isAxiosError: boolean;
|
|
|
|
|
2020-04-16 00:23:24 +08:00
|
|
|
/**
|
|
|
|
* 请求配置
|
|
|
|
*/
|
2020-04-17 00:18:59 +08:00
|
|
|
config: AxiosRequestConfig;
|
|
|
|
|
2020-04-16 00:23:24 +08:00
|
|
|
/**
|
2020-04-17 12:06:41 +08:00
|
|
|
* 响应体
|
2020-04-16 00:23:24 +08:00
|
|
|
*/
|
2020-04-14 23:45:21 +08:00
|
|
|
response?: AxiosResponse;
|
|
|
|
|
2020-04-17 00:18:59 +08:00
|
|
|
constructor(message: string, config: AxiosRequestConfig, response?: AxiosResponse) {
|
2020-04-14 23:45:21 +08:00
|
|
|
super(message);
|
|
|
|
|
2020-04-17 00:18:59 +08:00
|
|
|
this.isAxiosError = true;
|
2020-04-14 23:45:21 +08:00
|
|
|
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 请求响应体
|
|
|
|
*/
|
2020-04-17 00:18:59 +08:00
|
|
|
export default function createError(message: string, config: AxiosRequestConfig, response?: AxiosResponse): AxiosError {
|
2020-04-14 23:45:21 +08:00
|
|
|
return new AxiosError(message, config, response);
|
|
|
|
}
|