Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(projects): axios封装:文件夹规范,错误处理完善
- Loading branch information
Soybean
committed
Nov 22, 2021
1 parent
c81221e
commit 451c754
Showing
10 changed files
with
88 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#请求的环境 | ||
VITE_HTTP_ENV=DEV | ||
#请求地址 | ||
VITE_HTTP_URL=http://192.168.100.57/ | ||
VITE_HTTP_URL=http://192.168.100.43:8201 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#请求的环境 正式环境 | ||
VITE_HTTP_ENV=PROD | ||
#请求地址 | ||
VITE_HTTP_URL=http://119.23.220.176:17321 | ||
VITE_HTTP_URL=http://192.168.100.43:8201 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
VITE_HTTP_ENV=STAGING | ||
#请求地址 | ||
VITE_HTTP_URL=http://119.23.220.176:17321 | ||
VITE_HTTP_URL=http://192.168.100.43:8201 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
export {}; | ||
import { request } from '../request'; | ||
|
||
/** | ||
* 获取数据字典 | ||
*/ | ||
export async function fetchDictionary(keyword: string) { | ||
await request.post('/ehe/model/getByIndicator', { indiCatorName: keyword }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** 请求超时时间 */ | ||
export const REQUEST_TIMEOUT = 60 * 1000; | ||
|
||
/** 默认的请求错误文本 */ | ||
export const DEFAULT_REQUEST_ERROR_MSG = '请求错误~'; | ||
|
||
/** 请求超时的错误文本 */ | ||
export const REQUEST_TIMEOUT_MSG = '请求超时~'; | ||
|
||
/** 网络不可用的错误文本 */ | ||
export const NETWORK_ERROR_MSG = '网络不可用~'; | ||
|
||
/** 请求不成功各种状态的错误 */ | ||
export const ERROR_STATUS = { | ||
400: '400: 请求出现语法错误', | ||
401: '401: 用户未授权~', | ||
403: '403: 服务器拒绝访问~', | ||
404: '404: 请求的资源不存在~', | ||
405: '405: 请求方法未允许~', | ||
408: '408: 网络请求超时~', | ||
500: '500: 服务器内部错误~', | ||
501: '501: 服务器未实现请求功能~', | ||
502: '502: 错误网关~', | ||
503: '503: 服务不可用~', | ||
504: '504: 网关超时~', | ||
505: '505: http版本不支持该请求~' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,48 @@ | ||
const ERROR_STATUS = { | ||
400: '400: 请求出现语法错误', | ||
401: '401: 用户未授权~', | ||
403: '403: 服务器拒绝访问~', | ||
404: '404: 请求的资源不存在~', | ||
405: '405: 请求方法未允许~', | ||
408: '408: 网络请求超时~', | ||
500: '500: 服务器内部错误~', | ||
501: '501: 服务器未实现请求功能~', | ||
502: '502: 错误网关~', | ||
503: '503: 服务不可用~', | ||
504: '504: 网关超时~', | ||
505: '505: http版本不支持该请求~' | ||
}; | ||
import type { AxiosError, AxiosResponse } from 'axios'; | ||
import { DEFAULT_REQUEST_ERROR_MSG, ERROR_STATUS, NETWORK_ERROR_MSG } from '../config'; | ||
|
||
type ErrorStatus = keyof typeof ERROR_STATUS; | ||
|
||
type ErrorMsg = [boolean, string]; | ||
/** | ||
* 获取请求失败的错误 | ||
* @param error | ||
*/ | ||
export function getFailRequestErrorMsg(error: any) { | ||
const errorAction: ErrorMsg[] = [ | ||
[!window.navigator.onLine || error.message === 'Network Error', '网络不可用~'], | ||
[error.code === 'ECONNABORTED' && error.message.includes('timeout'), '网络连接超时~'], | ||
[error.response, ERROR_STATUS[error.response.status as ErrorStatus]] | ||
]; | ||
let errorMsg = '请求错误~'; | ||
errorAction.some(item => { | ||
const [flag, msg] = item; | ||
if (flag) { | ||
errorMsg = msg; | ||
} | ||
return flag; | ||
}); | ||
return errorMsg; | ||
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 handleResponseError(error: any) { | ||
export function handleAxiosError(error: AxiosError) { | ||
const { $message: Message } = window; | ||
|
||
const msg = getFailRequestErrorMsg(error); | ||
|
||
Message?.error(msg); | ||
} | ||
|
||
/** | ||
* 处理请求成功后的错误 | ||
* @param response - 请求的响应 | ||
*/ | ||
export function handleResponseError(response: AxiosResponse) { | ||
if (!window.navigator.onLine) { | ||
return NETWORK_ERROR_MSG; | ||
} | ||
const errorCode: ErrorStatus = response.status as ErrorStatus; | ||
const msg = ERROR_STATUS[errorCode] || DEFAULT_REQUEST_ERROR_MSG; | ||
return msg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { createRequest } from './axios'; | ||
import { REQUEST_TIMEOUT, REQUEST_TIMEOUT_MSG } from './config'; | ||
|
||
export const adminRequest = createRequest({ | ||
baseURL: import.meta.env.VITE_HTTP_URL_EMOSS_ADMIN as string | ||
export const request = createRequest({ | ||
baseURL: import.meta.env.VITE_HTTP_URL, | ||
timeout: REQUEST_TIMEOUT, | ||
timeoutErrorMessage: REQUEST_TIMEOUT_MSG | ||
}); |
451c754
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: