Fix displaying messages for multiple errors

This commit is contained in:
Angelina Filippova 2021-03-02 00:22:26 +03:00
parent 8a39058467
commit 5ca76701c2

View file

@ -6,27 +6,32 @@ const service = axios.create({
timeout: 60000 // request timeout timeout: 60000 // request timeout
}) })
const isJson = ({ headers }) => headers['content-type'].includes('application/json')
// response interceptor // response interceptor
service.interceptors.response.use( service.interceptors.response.use(
response => response, response => response,
error => { error => {
let errorMessage
console.log(`Error ${error}`) console.log(`Error ${error}`)
if (error.response) { if (!error.response) {
const edata = error.response.data.error ? error.response.data.error : error.response.data Message({
errorMessage = !error.response.headers['content-type'].includes('application/json') message: error,
? `${error.message}` type: 'error',
: `${error.message} - ${edata}` duration: 5 * 1000
})
} else { } else {
errorMessage = error const errors = Array.isArray(error.response.data) ? error.response.data : [error.response.data]
errors.forEach(errorData => {
const edata = errorData.error || errorData
Message({
message: isJson(error.response) ? `${error.message} - ${edata}` : `${error.message}`,
type: 'error',
duration: 5 * 1000
})
})
} }
Message({
message: errorMessage,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error) return Promise.reject(error)
} }
) )