axios-better-stacktrace
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [1.2.0](https://github.com/svsool/axios-better-stacktrace/compare/v1.1.0...v1.2.0) (2021-02-07) | ||
### Features | ||
* Add exposeTopmostErrorViaConfig option ([#1](https://github.com/svsool/axios-better-stacktrace/issues/1)) ([0007920](https://github.com/svsool/axios-better-stacktrace/commit/0007920c8a4d0448db8e13cb3574c39ace777bfe)) | ||
## 1.1.0 (2020-12-03) | ||
@@ -7,0 +14,0 @@ |
@@ -1,12 +0,11 @@ | ||
declare type AxiosLikeInstance = { | ||
post: Function; | ||
put: Function; | ||
patch: Function; | ||
delete: Function; | ||
get: Function; | ||
head: Function; | ||
options: Function; | ||
request: Function; | ||
}; | ||
declare const axiosBetterStacktrace: (axiosInstance?: AxiosLikeInstance | undefined) => (() => void) | undefined; | ||
import { AxiosInstance } from 'axios'; | ||
declare module 'axios' { | ||
interface AxiosRequestConfig { | ||
topmostError?: Error; | ||
} | ||
} | ||
declare const axiosBetterStacktrace: (axiosInstance?: AxiosInstance | undefined, opts?: { | ||
errorMsg?: string | undefined; | ||
exposeTopmostErrorViaConfig?: boolean | undefined; | ||
}) => (() => void) | undefined; | ||
export default axiosBetterStacktrace; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var axiosMethods = [ | ||
'request', | ||
'get', | ||
'delete', | ||
'head', | ||
'options', | ||
'post', | ||
'put', | ||
'patch', | ||
'delete', | ||
'get', | ||
'head', | ||
'options', | ||
'request', | ||
]; | ||
var axiosBetterStacktrace = function (axiosInstance) { | ||
var axiosBetterStacktraceHandler = function (params, topmostError, exposeTopmostErrorViaConfig) { | ||
// extend config with topmostError which can be used | ||
// inside interceptor callbacks (e.g. for logging purposes) | ||
var handlerResult = (function () { | ||
switch (params.method) { | ||
case 'request': { | ||
var originalHandler = params.originalHandler, config = params.args[0]; | ||
return originalHandler(exposeTopmostErrorViaConfig | ||
? __assign(__assign({}, (config || {})), { topmostError: topmostError }) : config); | ||
} | ||
case 'get': | ||
case 'delete': | ||
case 'head': | ||
case 'options': { | ||
var originalHandler = params.originalHandler, _a = params.args, url = _a[0], config = _a[1]; | ||
return originalHandler(url, exposeTopmostErrorViaConfig | ||
? __assign(__assign({}, (config || {})), { topmostError: topmostError }) : config); | ||
} | ||
case 'post': | ||
case 'put': | ||
case 'patch': { | ||
var originalHandler = params.originalHandler, _b = params.args, url = _b[0], data = _b[1], config = _b[2]; | ||
return originalHandler(url, data, exposeTopmostErrorViaConfig | ||
? __assign(__assign({}, (config || {})), { topmostError: topmostError }) : config); | ||
} | ||
} | ||
})(); | ||
// extend axios original handlers with a catch block which augments original error with a better stack trace | ||
return handlerResult.catch(function (maybeError) { | ||
if (maybeError instanceof Error) { | ||
var error = maybeError; | ||
error.originalStack = maybeError.stack; | ||
error.stack = error.stack + "\n" + topmostError.stack; | ||
throw error; | ||
} | ||
return maybeError; | ||
}); | ||
}; | ||
var axiosBetterStacktrace = function (axiosInstance, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
var _a = opts.errorMsg, errorMsg = _a === void 0 ? 'Axios Better Stacktrace' : _a, _b = opts.exposeTopmostErrorViaConfig, exposeTopmostErrorViaConfig = _b === void 0 ? false : _b; | ||
// do nothing if input does not look like an axios instance | ||
@@ -29,25 +80,44 @@ if (!axiosInstance || !axiosMethods.some(function (method) { return axiosInstance.hasOwnProperty(method); })) { | ||
}, {}); | ||
// extend axios original handlers with a catch block which augments original error with a better stack trace | ||
axiosMethods.forEach(function (method) { | ||
if (method in axiosInstance) { | ||
var methodHandler_1 = axiosInstance[method]; | ||
axiosInstance[method] = function axiosBetterStacktraceMethodProxy() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
switch (method) { | ||
case 'request': { | ||
var originalHandler_1 = axiosInstance[method]; | ||
axiosInstance[method] = function axiosBetterStacktraceMethodProxy(config) { | ||
return axiosBetterStacktraceHandler({ | ||
method: method, | ||
originalHandler: originalHandler_1, | ||
args: [config], | ||
}, new Error(errorMsg), exposeTopmostErrorViaConfig); | ||
}; | ||
break; | ||
} | ||
// obtain handler's stack trace at the topmost level possible | ||
// to provide more details than the original error | ||
var topmostStack = new Error('Axios Better Stacktrace').stack; | ||
var handlerResult = methodHandler_1.apply(void 0, args); | ||
return handlerResult.catch(function (maybeError) { | ||
if (maybeError instanceof Error) { | ||
var error = maybeError; | ||
error.originalStack = maybeError.stack; | ||
error.stack = error.stack + "\n" + topmostStack; | ||
throw error; | ||
} | ||
return maybeError; | ||
}); | ||
}; | ||
case 'get': | ||
case 'delete': | ||
case 'head': | ||
case 'options': { | ||
var originalHandler_2 = axiosInstance[method]; | ||
axiosInstance[method] = function axiosBetterStacktraceMethodProxy(url, config) { | ||
return axiosBetterStacktraceHandler({ | ||
method: method, | ||
originalHandler: originalHandler_2, | ||
args: [url, config], | ||
}, new Error(errorMsg), exposeTopmostErrorViaConfig); | ||
}; | ||
break; | ||
} | ||
case 'post': | ||
case 'put': | ||
case 'patch': { | ||
var originalHandler_3 = axiosInstance[method]; | ||
axiosInstance[method] = function axiosBetterStacktraceMethodProxy(url, data, config) { | ||
return axiosBetterStacktraceHandler({ | ||
method: method, | ||
originalHandler: originalHandler_3, | ||
args: [url, data, config], | ||
}, new Error(errorMsg), exposeTopmostErrorViaConfig); | ||
}; | ||
break; | ||
} | ||
} | ||
if (!axiosInstance.__axiosBetterStacktracePatched) { | ||
@@ -54,0 +124,0 @@ axiosInstance.__axiosBetterStacktracePatched = true; |
@@ -6,2 +6,3 @@ { | ||
"axios", | ||
"axios-plugin", | ||
"plugin", | ||
@@ -14,3 +15,3 @@ "stacktrace", | ||
"author": "Svyatoslav Sobol <svyatoslav.sobol@gmail.com>", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"main": "lib/axiosBetterStacktrace.js", | ||
@@ -60,3 +61,6 @@ "types": "lib/axiosBetterStacktrace.d.ts", | ||
"typescript": "^4.1.2" | ||
}, | ||
"peerDependencies": { | ||
"axios": "^0.21.0" | ||
} | ||
} |
@@ -32,3 +32,3 @@ # axios-better-stacktrace | ||
// CommonJS | ||
// const axiosBetterStacktrace = require('axios-better-stacktrace'); | ||
// const axiosBetterStacktrace = require('axios-better-stacktrace').default; | ||
@@ -38,8 +38,33 @@ // ES6 | ||
axiosBetterStacktrace(axios); | ||
// Example 1 | ||
axiosBetterStacktrace(axiosAgent); | ||
// response error will get an enhanced stack trace inside a catch block automatically | ||
axios.get('https://npmjs.com/<not-found>/').catch(enhancedError => console.error(enhancedError)); | ||
// Example 2 | ||
// enhanced error can also be accessed inside an interceptor callback (e.g. for logging purposes) | ||
axiosBetterStacktrace(axiosAgent, { exposeTopmostErrorViaConfig: true }); | ||
axios.interceptors.response.use(config => config, (result) => { | ||
console.error(result.config.topmostError); | ||
return result; | ||
}); | ||
axios.get('https://npmjs.com/<not-found>/'); | ||
``` | ||
## Options | ||
| Name | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| errorMsg | `String` | `Axios Better Stacktrace` | Error message to show next to the original one in the output. | | ||
| exposeTopmostErrorViaConfig | `Boolean` | `false` | Whether to expose enhanced error as `config.topmostError`. See [this issue](https://github.com/svsool/axios-better-stacktrace/issues/1) for reference. | | ||
## Example | ||
Axios error without axios-better-stacktrace plugin: | ||
Axios error without an axios-better-stacktrace plugin: | ||
@@ -56,3 +81,3 @@ ``` | ||
Axios error with axios-better-stacktrace plugin: | ||
Axios error with an axios-better-stacktrace plugin: | ||
@@ -59,0 +84,0 @@ ``` |
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
17179
146
101
1