@redtea/format-axios-error
Advanced tools
Comparing version 2.0.2 to 2.1.0
import { AxiosError } from 'axios'; | ||
import { AxiosErrorFormatError } from './axios-error-format'; | ||
export declare function format<T = any>(error: AxiosError<T>): AxiosErrorFormatError<T>; | ||
export declare function format<E = any>(error: E): E extends AxiosError<infer T> ? AxiosErrorFormatError<T> : E; | ||
export declare function isAxiosError(error: any): error is AxiosError; |
18
core.js
@@ -7,3 +7,3 @@ "use strict"; | ||
if (!isAxiosError(error)) { | ||
throw new TypeError('Invalid axios error'); | ||
return error; | ||
} | ||
@@ -18,3 +18,5 @@ const formatError = new Error(error.message); | ||
if (error.config[configKey] !== undefined) { | ||
formatError.config[configKey] = error.config[configKey]; | ||
formatError.config[configKey] = configKey === 'data' | ||
? formatData(error.config[configKey]) | ||
: error.config[configKey]; | ||
} | ||
@@ -26,3 +28,5 @@ } | ||
if (error.response[responseKey] !== undefined) { | ||
formatError.response[responseKey] = error.response[responseKey]; | ||
formatError.response[responseKey] = responseKey === 'data' | ||
? formatData(error.response[responseKey]) | ||
: error.response[responseKey]; | ||
} | ||
@@ -34,2 +38,10 @@ } | ||
exports.format = format; | ||
function formatData(data) { | ||
if (global && global.process) { | ||
if (data instanceof require('stream').Readable) { | ||
return '[Readable]'; | ||
} | ||
} | ||
return data; | ||
} | ||
function isAxiosError(error) { | ||
@@ -36,0 +48,0 @@ return !!(error === null || error === void 0 ? void 0 : error.isAxiosError); |
@@ -1,2 +0,4 @@ | ||
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f():typeof define==='function'&&define.amd?define(f):(g=typeof globalThis!=='undefined'?globalThis:g||self,g.AxiosErrorFormat=f());}(this,(function(){'use strict';function getDefaultExportFromCjs (x) { | ||
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('stream')):typeof define==='function'&&define.amd?define(['stream'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,g.AxiosErrorFormat=f(g.require$$0));}(this,(function(require$$0){'use strict';function _interopDefaultLegacy(e){return e&&typeof e==='object'&&'default'in e?e:{'default':e}}var require$$0__default=/*#__PURE__*/_interopDefaultLegacy(require$$0);var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
function getDefaultExportFromCjs (x) { | ||
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; | ||
@@ -106,3 +108,3 @@ } | ||
if (!isAxiosError(error)) { | ||
throw new TypeError('Invalid axios error'); | ||
return error; | ||
} | ||
@@ -125,3 +127,3 @@ | ||
if (error.config[configKey] !== undefined) { | ||
formatError.config[configKey] = error.config[configKey]; | ||
formatError.config[configKey] = configKey === 'data' ? formatData(error.config[configKey]) : error.config[configKey]; | ||
} | ||
@@ -146,3 +148,3 @@ } | ||
if (error.response[responseKey] !== undefined) { | ||
formatError.response[responseKey] = error.response[responseKey]; | ||
formatError.response[responseKey] = responseKey === 'data' ? formatData(error.response[responseKey]) : error.response[responseKey]; | ||
} | ||
@@ -162,2 +164,12 @@ } | ||
function formatData(data) { | ||
if (commonjsGlobal && commonjsGlobal.process) { | ||
if (data instanceof require$$0__default['default'].Readable) { | ||
return '[Readable]'; | ||
} | ||
} | ||
return data; | ||
} | ||
function isAxiosError(error) { | ||
@@ -164,0 +176,0 @@ return !!(error === null || error === void 0 ? void 0 : error.isAxiosError); |
{ | ||
"name": "@redtea/format-axios-error", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Pretty format axios error. Can by used with logform and winston.", | ||
@@ -5,0 +5,0 @@ "type": "commonjs", |
# format-axios-error | ||
Pretty format [axios error](https://github.com/axios/axios#handling-errors). Can be used with [logform](https://github.com/winstonjs/logform) and [winston](https://github.com/winstonjs/winston). | ||
Format [axios error](https://github.com/axios/axios#handling-errors) for logs. Support for [logform](https://github.com/winstonjs/logform) and [winston](https://github.com/winstonjs/winston). | ||
# Motivation | ||
An axios error contains a lot of information that is not useful in logs. `format-axios-error` is created to solve that problem. It takes original axios error instance and create new one cutting redundant data. | ||
# Installation | ||
@@ -21,3 +25,3 @@ | ||
After formatting you will get another instance of axios error. Formatter will strip values of function type, request object and other data that not useful in logs. | ||
There are three ways how to use: | ||
@@ -62,8 +66,8 @@ ### NodeJS | ||
``` | ||
_Response data and headers omitted in this example. To see full log message you can run code example locally_ | ||
_Response data and headers in this example are omitted. To see full log message run the code snipped locally_ | ||
### Winston | ||
### Winston and logform | ||
This example show how provide formatter for winston, but you can use the same module for logform. | ||
The following example shows how to use the library with [winston](https://github.com/winstonjs/winston) logger and [logform](https://github.com/winstonjs/logform). | ||
@@ -70,0 +74,0 @@ ```javascript |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22698
471
146