mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Fixed #336
This commit is contained in:
38
src/api/handleError.ts
Normal file
38
src/api/handleError.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import chalk from 'chalk'
|
||||
|
||||
export const ctx = new chalk.Instance({ level: 3 })
|
||||
|
||||
const handleError = (error: any) => {
|
||||
if (error?.response) {
|
||||
// The request was made and the server responded with a status code
|
||||
// that falls out of the range of 2xx
|
||||
console.error(
|
||||
ctx.bold(' API instance '),
|
||||
ctx.bold('response'),
|
||||
error.response.status,
|
||||
error?.response.data?.error || error?.response.message || 'Unknown error'
|
||||
)
|
||||
return Promise.reject({
|
||||
status: error?.response.status,
|
||||
message:
|
||||
error?.response.data?.error ||
|
||||
error?.response.message ||
|
||||
'Unknown error'
|
||||
})
|
||||
} else if (error?.request) {
|
||||
// The request was made but no response was received
|
||||
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
||||
// http.ClientRequest in node.js
|
||||
console.error(ctx.bold(' API instance '), ctx.bold('request'), error)
|
||||
return Promise.reject()
|
||||
} else {
|
||||
console.error(
|
||||
ctx.bold(' API instance '),
|
||||
ctx.bold('internal'),
|
||||
error?.message
|
||||
)
|
||||
return Promise.reject()
|
||||
}
|
||||
}
|
||||
|
||||
export default handleError
|
Reference in New Issue
Block a user