Skip to content

Commit 333b763

Browse files
authored
Fix unhandled rejection handling (#2390)
* Fix unhandled rejection handlers * Fix unhandled rejection handlers * Fix typo
1 parent c3f3b5b commit 333b763

File tree

6 files changed

+372
-2101
lines changed

6 files changed

+372
-2101
lines changed

lib/winston.js

+11
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ Object.defineProperty(exports, 'exceptions', {
135135
}
136136
});
137137

138+
/**
139+
* Define getter for `rejections` which replaces `handleRejections` and
140+
* `unhandleRejections`.
141+
* @type {Object}
142+
*/
143+
Object.defineProperty(exports, 'rejections', {
144+
get() {
145+
return defaultLogger.rejections;
146+
}
147+
});
148+
138149
/**
139150
* Define getters / setters for appropriate properties of the default logger
140151
* which need to be exposed by winston.

lib/winston/rejection-handler.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const asyncForEach = require('async/forEach');
1212
const debug = require('@dabh/diagnostics')('winston:rejection');
1313
const once = require('one-time');
1414
const stackTrace = require('stack-trace');
15-
const ExceptionStream = require('./exception-stream');
15+
const RejectionStream = require('./rejection-stream');
1616

1717
/**
1818
* Object for handling unhandledRejection events.
@@ -88,7 +88,7 @@ module.exports = class RejectionHandler {
8888
err && err.stack || ' No stack trace'
8989
].join('\n'),
9090
stack: err && err.stack,
91-
exception: true,
91+
rejection: true,
9292
date: new Date().toString(),
9393
process: this.getProcessInfo(),
9494
os: this.getOsInfo(),
@@ -151,7 +151,7 @@ module.exports = class RejectionHandler {
151151
_addHandler(handler) {
152152
if (!this.handlers.has(handler)) {
153153
handler.handleRejections = true;
154-
const wrapper = new ExceptionStream(handler);
154+
const wrapper = new RejectionStream(handler);
155155
this.handlers.set(handler, wrapper);
156156
this.logger.pipe(wrapper);
157157
}

lib/winston/rejection-stream.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* rejection-stream.js: TODO: add file header handler.
3+
*
4+
* (C) 2010 Charlie Robbins
5+
* MIT LICENCE
6+
*/
7+
8+
'use strict';
9+
10+
const { Writable } = require('readable-stream');
11+
12+
/**
13+
* TODO: add class description.
14+
* @type {RejectionStream}
15+
* @extends {Writable}
16+
*/
17+
module.exports = class RejectionStream extends Writable {
18+
/**
19+
* Constructor function for the RejectionStream responsible for wrapping a
20+
* TransportStream; only allowing writes of `info` objects with
21+
* `info.rejection` set to true.
22+
* @param {!TransportStream} transport - Stream to filter to rejections
23+
*/
24+
constructor(transport) {
25+
super({ objectMode: true });
26+
27+
if (!transport) {
28+
throw new Error('RejectionStream requires a TransportStream instance.');
29+
}
30+
31+
this.handleRejections = true;
32+
this.transport = transport;
33+
}
34+
35+
/**
36+
* Writes the info object to our transport instance if (and only if) the
37+
* `rejection` property is set on the info.
38+
* @param {mixed} info - TODO: add param description.
39+
* @param {mixed} enc - TODO: add param description.
40+
* @param {mixed} callback - TODO: add param description.
41+
* @returns {mixed} - TODO: add return description.
42+
* @private
43+
*/
44+
_write(info, enc, callback) {
45+
if (info.rejection) {
46+
return this.transport.log(info, callback);
47+
}
48+
49+
callback();
50+
return true;
51+
}
52+
};

0 commit comments

Comments
 (0)