Pretty format axios error. Can by used with logform and winston.
npm install @redtea/format-axios-errorThis is a small library that provides a function to format an axios error object) for logging purposes. It strips any unnecessary information from the error object, making it more readable and easy to understand when reviewing logs.
Can be used with logform and winston.
Yarn
``bash`
$ yarn add -E @redtea/format-axios-error
NPM
`bash`
$ npm install -E @redtea/format-axios-error
More usage examples can be found in examples directory.
`javascript
const axios = require('axios');
const {format} = require('@redtea/format-axios-error');
axios
.get('https://google.com/give-404')
.catch(error => {
console.log(
JSON.stringify(format(error), null, 2)
)
});
``
will print
{
"name": "Error",
"isAxiosError": true,
"config": {
"url": "https://google.com/give-404",
"method": "get",
"headers": [headers],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1
},
"response": {
"data": [data],
"status": 404,
"statusText": "Not Found",
"headers": [headers]
}
}
`
_In the example, response data and headers are omitted. Run the code snipped locally to see full log output._
`javascript
const winston = require('winston');
const axios = require('axios');
const {axiosError} = require('@redtea/format-axios-error/logform');
const {combine, json} = winston.format;
const logger = winston.createLogger({
level: 'info',
format: combine(
axiosError(),
json({ space: 2 })
),
transports: [
new winston.transports.Console(),
],
});
axios
.get('https://google.com/give-404')
.catch(error => logger.error(error))
``
will print`
{
"config": {
"url": "https://google.com/give-404",
"method": "get",
"headers": [headers],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1
},
"isAxiosError": true,
"level": "error",
"response": {
"status": 404,
"data": [data],
"headers": [headers],
"statusText": "Not Found"
},
"message": "Request failed with status code 404"
}
`html``