axios-miniprogram/src/core/createError.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

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);
}