Skip to content

Commit

Permalink
Fix unhandled rejection handling (#2390)
Browse files Browse the repository at this point in the history
* Fix unhandled rejection handlers

* Fix unhandled rejection handlers

* Fix typo
  • Loading branch information
DABH authored Jan 4, 2024
1 parent c3f3b5b commit 333b763
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 2,101 deletions.
11 changes: 11 additions & 0 deletions lib/winston.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ Object.defineProperty(exports, 'exceptions', {
}
});

/**
* Define getter for `rejections` which replaces `handleRejections` and
* `unhandleRejections`.
* @type {Object}
*/
Object.defineProperty(exports, 'rejections', {
get() {
return defaultLogger.rejections;
}
});

/**
* Define getters / setters for appropriate properties of the default logger
* which need to be exposed by winston.
Expand Down
6 changes: 3 additions & 3 deletions lib/winston/rejection-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const asyncForEach = require('async/forEach');
const debug = require('@dabh/diagnostics')('winston:rejection');
const once = require('one-time');
const stackTrace = require('stack-trace');
const ExceptionStream = require('./exception-stream');
const RejectionStream = require('./rejection-stream');

/**
* Object for handling unhandledRejection events.
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports = class RejectionHandler {
err && err.stack || ' No stack trace'
].join('\n'),
stack: err && err.stack,
exception: true,
rejection: true,
date: new Date().toString(),
process: this.getProcessInfo(),
os: this.getOsInfo(),
Expand Down Expand Up @@ -151,7 +151,7 @@ module.exports = class RejectionHandler {
_addHandler(handler) {
if (!this.handlers.has(handler)) {
handler.handleRejections = true;
const wrapper = new ExceptionStream(handler);
const wrapper = new RejectionStream(handler);
this.handlers.set(handler, wrapper);
this.logger.pipe(wrapper);
}
Expand Down
52 changes: 52 additions & 0 deletions lib/winston/rejection-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* rejection-stream.js: TODO: add file header handler.
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*/

'use strict';

const { Writable } = require('readable-stream');

/**
* TODO: add class description.
* @type {RejectionStream}
* @extends {Writable}
*/
module.exports = class RejectionStream extends Writable {
/**
* Constructor function for the RejectionStream responsible for wrapping a
* TransportStream; only allowing writes of `info` objects with
* `info.rejection` set to true.
* @param {!TransportStream} transport - Stream to filter to rejections
*/
constructor(transport) {
super({ objectMode: true });

if (!transport) {
throw new Error('RejectionStream requires a TransportStream instance.');
}

this.handleRejections = true;
this.transport = transport;
}

/**
* Writes the info object to our transport instance if (and only if) the
* `rejection` property is set on the info.
* @param {mixed} info - TODO: add param description.
* @param {mixed} enc - TODO: add param description.
* @param {mixed} callback - TODO: add param description.
* @returns {mixed} - TODO: add return description.
* @private
*/
_write(info, enc, callback) {
if (info.rejection) {
return this.transport.log(info, callback);
}

callback();
return true;
}
};
Loading

0 comments on commit 333b763

Please sign in to comment.