项目作者: svsool

项目描述 :
Axios plugin that provides better stack traces for axios errors
高级语言: TypeScript
项目地址: git://github.com/svsool/axios-better-stacktrace.git
创建时间: 2020-12-01T19:31:49Z
项目社区:https://github.com/svsool/axios-better-stacktrace

开源协议:MIT License

下载


axios-better-stacktrace

Axios plugin that provides better stack traces for axios errors.

Check this axios issue for more details.

npm
npm
ci
npm

Installation

NPM

  1. npm install axios-better-stacktrace

Yarn

  1. yarn add axios-better-stacktrace

Note

It was tested with axios 0.21.0.

Usage

  1. // CommonJS
  2. // const axiosBetterStacktrace = require('axios-better-stacktrace').default;
  3. // ES6
  4. import axiosBetterStacktrace from 'axios-better-stacktrace';
  5. // use it before any other interceptors
  6. axiosBetterStacktrace(axiosAgent);
  7. // when using promises response error will get an enhanced stack trace automatically
  8. axiosAgent.get('https://npmjs.com/<not-found>/').catch(enhancedError => console.error(enhancedError));
  9. // or using async/await
  10. (async () => {
  11. try {
  12. await axiosAgent.get('https://npmjs.com/<not-found>/');
  13. } catch (enhancedError) {
  14. console.error(enhancedError);
  15. }
  16. })();
  17. // or using a response interceptor and an error callback (e.g. could be useful with a logging middleware)
  18. axiosAgent.interceptors.response.use(response => response, enhancedError => {
  19. console.error(enhancedError);
  20. return result;
  21. });
  22. // you can restore original agent behavior if needed
  23. const restoreAgent = axiosBetterStacktrace(axiosAgent);
  24. // some code here...
  25. restoreAgent && restoreAgent();

See also demo.

Options

Name Type Default Description
errorMsg String Axios Better Stacktrace Error message to show next to the original one in the output.

Example

Default axios error without an axios-better-stacktrace plugin:

  1. Error: Request failed with status code 404
  2. at createError (./node_modules/axios/lib/core/createError.js:16:15)
  3. at settle (./node_modules/axios/lib/core/settle.js:17:12)
  4. at IncomingMessage.handleStreamEnd (./node_modules/axios/lib/adapters/http.js:244:11)
  5. at IncomingMessage.emit (node:events:388:22)
  6. at IncomingMessage.EventEmitter.emit (node:domain:470:12)
  7. at endReadableNT (node:internal/streams/readable:1294:12)
  8. at processTicksAndRejections (node:internal/process/task_queues:80:21)

Enhanced axios error with an axios-better-stacktrace plugin (run yarn demo to see):

  1. Error: Request failed with status code 404
  2. at createError (./node_modules/axios/lib/core/createError.js:16:15)
  3. at settle (./node_modules/axios/lib/core/settle.js:17:12)
  4. at IncomingMessage.handleStreamEnd (./node_modules/axios/lib/adapters/http.js:244:11)
  5. at IncomingMessage.emit (node:events:388:22)
  6. at IncomingMessage.EventEmitter.emit (node:domain:470:12)
  7. at endReadableNT (node:internal/streams/readable:1294:12)
  8. at processTicksAndRejections (node:internal/process/task_queues:80:21)
  9. Error: Axios Better Stacktrace
  10. at Function.axiosBetterStacktraceMethodProxy [as get] (./src/axiosBetterStacktrace.ts:167:15)
  11. at getNpmPage (./demo/index.ts:10:35) <---- this is what usually useful to know for further debugging and this plugin adds 🙂
  12. at ./demo/index.ts:13:9
  13. at step (./demo/index.ts:33:23)
  14. at Object.next (./demo/index.ts:14:53)
  15. at ./demo/index.ts:8:71
  16. at new Promise (<anonymous>)
  17. at __awaiter (./demo/index.ts:4:12)
  18. at ./demo/index.ts:12:2
  19. at Object.<anonymous> (./demo/index.ts:16:3)