mirror of https://github.com/halo-dev/halo
Give a more error detail
parent
17c362a2d1
commit
3549929ea1
|
@ -2,9 +2,10 @@ import axios from 'axios'
|
||||||
import NProgress from 'nprogress'
|
import NProgress from 'nprogress'
|
||||||
import 'nprogress/nprogress.css'
|
import 'nprogress/nprogress.css'
|
||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import { message } from 'ant-design-vue'
|
import { message, notification } from 'ant-design-vue'
|
||||||
import store from '@/store'
|
import store from '@/store'
|
||||||
import router from '@/router'
|
import router from '@/router'
|
||||||
|
import { isObject } from './util'
|
||||||
|
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
baseURL: process.env.NODE_ENV === 'production' ? '' : 'http://localhost:8090',
|
baseURL: process.env.NODE_ENV === 'production' ? '' : 'http://localhost:8090',
|
||||||
|
@ -50,6 +51,16 @@ async function refreshToken(error) {
|
||||||
return reRequest(error)
|
return reRequest(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getFieldValidationError(data) {
|
||||||
|
if (!isObject(data) || !isObject(data.data)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const errorDetail = data.data
|
||||||
|
|
||||||
|
return Object.keys(errorDetail).map(key => errorDetail[key])
|
||||||
|
}
|
||||||
|
|
||||||
service.interceptors.request.use(
|
service.interceptors.request.use(
|
||||||
config => {
|
config => {
|
||||||
NProgress.start()
|
NProgress.start()
|
||||||
|
@ -84,10 +95,33 @@ service.interceptors.response.use(
|
||||||
|
|
||||||
const data = response ? response.data : null
|
const data = response ? response.data : null
|
||||||
if (data) {
|
if (data) {
|
||||||
|
let handled = false
|
||||||
// Business response
|
// Business response
|
||||||
Vue.$log.error('Business response status', data.status)
|
Vue.$log.error('Business response status', data.status)
|
||||||
if (data.status === 400) {
|
if (data.status === 400) {
|
||||||
// TODO handle 400 status error
|
// TODO handle 400 status error
|
||||||
|
const errorDetails = getFieldValidationError(data)
|
||||||
|
if (errorDetails) {
|
||||||
|
handled = true
|
||||||
|
|
||||||
|
notification.error({
|
||||||
|
message: data.message,
|
||||||
|
description: h => {
|
||||||
|
const errorNodes = errorDetails.map(errorDetail => {
|
||||||
|
return h('a-alert', {
|
||||||
|
props: {
|
||||||
|
message: errorDetail,
|
||||||
|
banner: true,
|
||||||
|
showIcon: false,
|
||||||
|
type: 'error'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return h('div', errorNodes)
|
||||||
|
},
|
||||||
|
duration: 10
|
||||||
|
})
|
||||||
|
}
|
||||||
} else if (data.status === 401) {
|
} else if (data.status === 401) {
|
||||||
// TODO handle 401 status error
|
// TODO handle 401 status error
|
||||||
if (store.getters.token && store.getters.token.access_token === data.data) {
|
if (store.getters.token && store.getters.token.access_token === data.data) {
|
||||||
|
@ -107,7 +141,9 @@ service.interceptors.response.use(
|
||||||
// TODO handle 500 status error
|
// TODO handle 500 status error
|
||||||
}
|
}
|
||||||
|
|
||||||
message.error(data.message)
|
if (!handled) {
|
||||||
|
message.error(data.message)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
message.error('Server unavailable')
|
message.error('Server unavailable')
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,3 +50,7 @@ export function timeAgo(time) {
|
||||||
return moment(time).format('YYYY-MM-DD HH:mm')
|
return moment(time).format('YYYY-MM-DD HH:mm')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isObject(value) {
|
||||||
|
return value && typeof value === 'object' && value.constructor === Object
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue