Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
improved parse axios error
Browse files Browse the repository at this point in the history
  • Loading branch information
ildella committed Jul 26, 2023
1 parent b658079 commit e43381e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions errors/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
const {pipeline, __} = require('../fusto')

// eslint-disable-next-line max-lines-per-function
// eslint-disable-next-line complexity
const parseAxiosError = error => {
if (error.isAxiosError !== true) return error
const {status, code, config} = error.toJSON()
const basic = __([config]).pick([
'url',
'method',
'baseURL',
'url',
'timeout',
// 'headers',
]).value()
// console.log({...basic, status, code})
if (code === 'ETIMEDOUT') return {
status: 408,
code,
message: 'Timed out.',
...basic,
}
const {response: {data}} = error
if (!error.response) return {
status, code, message: 'no response...', ...basic,
}
const {response} = error
// console.log(response.status)
// console.log(response.statusText)
const {data} = response
const {
// statusCode,
error: dataError = {},
message,
} = data
const finalMessage =
message || dataError.message || 'no message provided in the response, check the server logs'
const {method, url} = basic
const {method, url, baseURL} = basic
const humanReadableMessage =
`${status} ${method} ${url} :: ${finalMessage}`
// console.log({humanReadableMessage})
`${status} ${method} ${baseURL}${url} :: ${finalMessage}`
return {
status,
message: humanReadableMessage,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moar-js",
"version": "0.2.0",
"version": "0.2.1",
"description": "Simple JavaScript files I use across projects",
"license": "MIT",
"author": {
Expand Down

0 comments on commit e43381e

Please sign in to comment.