fetch 异常处理

master
Harry Yang 2023-01-03 14:59:58 +08:00
parent 49433609d1
commit 64010ff9c8
1 changed files with 33 additions and 13 deletions

View File

@ -158,6 +158,21 @@
return !isBlank(obj)
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response
}
else {
const error = new Error(response.statusText);
error.response = response
throw error
}
}
function parseJSON(response) {
return response.json()
}
const data = () => {
return {
auditForm: {
@ -262,19 +277,24 @@
'Content-Type': 'application/json',
},
body: JSON.stringify(form),
}).then(res => res.json())
.then(data => {
this.page = {
data: data.content,
size: data.size,
current: data.number + 1,
total: data.totalElements
}
})
.catch(err => {
this.$message.error('查询失败');
})
.finally(() => loading.close())
}).then(checkStatus).then(parseJSON).then(data => {
this.page = {
data: data.content,
size: data.size,
current: data.number + 1,
total: data.totalElements
}
}).catch(({ response }) => {
if (response) {
parseJSON(response)
.then(json => {
this.$message.error(json.message || "查询失败")
})
}
else {
this.$message.error("查询失败")
}
}).finally(() => loading.close())
},
handlePageChange(val) {