sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.29k stars 935 forks source link

Mention how to nicely capture event emitter errors #1854

Open szmarczak opened 3 years ago

szmarczak commented 3 years ago

Something like

const { EventEmitter, errorMonitor } = require('events');

EventEmitter.realInit = EventEmitter.init;
EventEmitter.init = function(opts) {
  Error.captureStackTrace(this);

  this.on(errorMonitor, error => {
    error.creationStack = this.stack;
  });

  return EventEmitter.realInit.call(this, opts);
};

// Demo time!
const https = require('https');
const request = https.request('https://example.com');
request.destroy();
sindresorhus commented 3 years ago

Should we really recommend monkey-patching EventEmitter? I would much prefer if we would open an issue on Node.js with a proposal for a public API to make this easy.

szmarczak commented 3 years ago

Agreed, I'll open an issue tomorrow

szmarczak commented 3 years ago

https://github.com/nodejs/node/issues/39917