update 手动抛出的异常返回的code仍然为200

This commit is contained in:
2021-06-30 11:54:23 +08:00
parent 35cb452d09
commit 635dd52270
4 changed files with 63 additions and 27 deletions

View File

@@ -13,7 +13,7 @@ import status from './status'
* api.getItemGroupType(parmas).then(...)
*/
import urls from './requests'
import { notification } from 'antd'
import { message as Message, notification } from 'antd'
const STATUS = status
@@ -64,6 +64,10 @@ const errorNotification = ({ code, message }) => {
}
}
const errorMessage = (message) => {
Message.error(message)
}
const handlerUnauthorized = () => {
token.value = ''
window.location.replace('/login')
@@ -125,13 +129,28 @@ for (let key in urls) {
api[`${key}Await`](params)
.then((res) => {
const { data } = res
const isFile = [ArrayBuffer, Blob].includes(data.constructor)
const result = isFile ? res : data
// 错误的返回码,以通知的形式弹出
if (errerCodes.indexOf(data.code) >= 0) {
errorNotification(data)
reject([ArrayBuffer, Blob].indexOf(data.constructor) > -1 ? res : data)
} else if (data.code === STATUS.Unauthorized) {
reject(result)
}
// 非文件,返回码正确,但是结果失败,以消息的形式弹出
else if (!isFile && !data.success) {
errorMessage(data.message)
reject(result)
}
// 未登录
else if (data.code === STATUS.Unauthorized) {
handlerUnauthorized()
} else {
reslove([ArrayBuffer, Blob].indexOf(data.constructor) > -1 ? res : data)
}
else {
reslove(result)
}
})
.catch(({ response }) => {