mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-12-25 05:50:18 +08:00
refactor(projects): axios封装完成
This commit is contained in:
@@ -1,37 +1,45 @@
|
||||
import type { AxiosError, AxiosResponse } from 'axios';
|
||||
import { DEFAULT_REQUEST_ERROR_MSG, ERROR_STATUS, NETWORK_ERROR_MSG } from '../config';
|
||||
import type { RequestServiceError, BackendServiceResult } from '@/interface';
|
||||
import {
|
||||
DEFAULT_REQUEST_ERROR_CODE,
|
||||
DEFAULT_REQUEST_ERROR_MSG,
|
||||
NETWORK_ERROR_CODE,
|
||||
NETWORK_ERROR_MSG,
|
||||
REQUEST_TIMEOUT_CODE,
|
||||
REQUEST_TIMEOUT_MSG,
|
||||
ERROR_STATUS
|
||||
} from '../config';
|
||||
import { showErrorMsg } from './msg';
|
||||
|
||||
type ErrorStatus = keyof typeof ERROR_STATUS;
|
||||
|
||||
/**
|
||||
* 获取请求失败的错误
|
||||
* @param error
|
||||
*/
|
||||
export function getFailRequestErrorMsg(error: AxiosError) {
|
||||
if (!window.navigator.onLine || error.message === 'Network Error') {
|
||||
return NETWORK_ERROR_MSG;
|
||||
}
|
||||
if (error.code === 'ECONNABORTED') {
|
||||
return error.message;
|
||||
}
|
||||
if (error.response) {
|
||||
const errorCode: ErrorStatus = error.response.status as ErrorStatus;
|
||||
const msg = ERROR_STATUS[errorCode] || DEFAULT_REQUEST_ERROR_MSG;
|
||||
return msg;
|
||||
}
|
||||
return DEFAULT_REQUEST_ERROR_MSG;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求失败的错误
|
||||
* @param error - 错误
|
||||
*/
|
||||
export function handleAxiosError(error: AxiosError) {
|
||||
const { $message: Message } = window;
|
||||
export function handleAxiosError(axiosError: AxiosError) {
|
||||
const error: RequestServiceError = {
|
||||
type: 'axios',
|
||||
code: DEFAULT_REQUEST_ERROR_CODE,
|
||||
msg: DEFAULT_REQUEST_ERROR_MSG
|
||||
};
|
||||
|
||||
const msg = getFailRequestErrorMsg(error);
|
||||
if (!window.navigator.onLine || axiosError.message === 'Network Error') {
|
||||
// 网路错误
|
||||
Object.assign(error, { code: NETWORK_ERROR_CODE, msg: NETWORK_ERROR_MSG });
|
||||
} else if (axiosError.code === REQUEST_TIMEOUT_CODE && axiosError.message.includes('timeout')) {
|
||||
/** 超时错误 */
|
||||
Object.assign(error, { code: REQUEST_TIMEOUT_CODE, msg: REQUEST_TIMEOUT_MSG });
|
||||
} else if (axiosError.response) {
|
||||
// 请求不成功的错误
|
||||
const errorCode: ErrorStatus = axiosError.response.status as ErrorStatus;
|
||||
const msg = ERROR_STATUS[errorCode] || DEFAULT_REQUEST_ERROR_MSG;
|
||||
Object.assign(error, { code: errorCode, msg });
|
||||
}
|
||||
|
||||
Message?.error(msg);
|
||||
showErrorMsg(error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,10 +47,39 @@ export function handleAxiosError(error: AxiosError) {
|
||||
* @param response - 请求的响应
|
||||
*/
|
||||
export function handleResponseError(response: AxiosResponse) {
|
||||
const error: RequestServiceError = {
|
||||
type: 'axios',
|
||||
code: DEFAULT_REQUEST_ERROR_CODE,
|
||||
msg: DEFAULT_REQUEST_ERROR_MSG
|
||||
};
|
||||
|
||||
if (!window.navigator.onLine) {
|
||||
return NETWORK_ERROR_MSG;
|
||||
// 网路错误
|
||||
Object.assign(error, { code: NETWORK_ERROR_CODE, msg: NETWORK_ERROR_MSG });
|
||||
} else {
|
||||
// 请求成功的状态码非200的错误
|
||||
const errorCode: ErrorStatus = response.status as ErrorStatus;
|
||||
const msg = ERROR_STATUS[errorCode] || DEFAULT_REQUEST_ERROR_MSG;
|
||||
Object.assign(error, { type: 'backend', code: errorCode, msg });
|
||||
}
|
||||
const errorCode: ErrorStatus = response.status as ErrorStatus;
|
||||
const msg = ERROR_STATUS[errorCode] || DEFAULT_REQUEST_ERROR_MSG;
|
||||
return msg;
|
||||
|
||||
showErrorMsg(error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理后端返回的错误
|
||||
* @param backendResult - 后端接口的响应数据
|
||||
*/
|
||||
export function handleBackendError(backendResult: BackendServiceResult) {
|
||||
const error: RequestServiceError = {
|
||||
type: 'backend',
|
||||
code: backendResult.code,
|
||||
msg: backendResult.message
|
||||
};
|
||||
|
||||
showErrorMsg(error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user