Closed jstewmon closed 5 years ago
I think you also need to listen for response's error event. For example, if there's an error reading the response body.
Fixed in 9fac585ca5863be0fec278dd2d772fd39f9e32a6
Was there a reason you wanted to patch emit
? prependListener
allows you to move to the front of the line if other listeners have already been attached.
Was there a reason you wanted to patch emit?
Yup. The implementation you're talking about is:
const handleError = origin => {
const errorHandler = error => {
timings.error = Date.now();
timings.phases.total = timings.error - timings.start;
origin.off('newListener', newListenerHandler); // eslint-disable-line no-use-before-define
if (origin.listenerCount('error') === 0) {
throw error;
}
};
const newListenerHandler = (event, handler) => {
if (event === 'error' && handler !== errorHandler) {
origin.off('error', errorHandler);
// Make sure our handler gets called first
process.nextTick(() => origin.prependOnceListener('error', errorHandler));
}
};
origin.on('newListener', newListenerHandler);
origin.once('error', errorHandler);
};
It's a bit complicated, isn't it? If you wanna send a PR, feel free :) I'm happy to work on this.
The current solution is much shorter and I know for sure it works :P
Pardon my very late response, but the approach I had shown was incorrect. I've updated the post above. Please take a look at it! :)
Error events are not listened for, so if the request or response terminates due to an error, total will not be set.