Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled rejection handling #2390

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't get used anywhere atm but for completeness it felt like we should have this to parallel exceptions right above?

get() {
return defaultLogger.rejections;
}
});

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

Check failure on line 15 in lib/winston/rejection-handler.js

View workflow job for this annotation

GitHub Actions / Tests (16)

'ExceptionStream' is assigned a value but never used

Check failure on line 15 in lib/winston/rejection-handler.js

View workflow job for this annotation

GitHub Actions / Tests (18)

'ExceptionStream' is assigned a value but never used

Check failure on line 15 in lib/winston/rejection-handler.js

View workflow job for this annotation

GitHub Actions / Tests (20)

'ExceptionStream' is assigned a value but never used

/**
* Object for handling unhandledRejection events.
Expand Down Expand Up @@ -88,7 +88,7 @@
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 @@
_addHandler(handler) {
if (!this.handlers.has(handler)) {
handler.handleRejections = true;
const wrapper = new ExceptionStream(handler);
const wrapper = new RejectionStream(handler);

Check failure on line 154 in lib/winston/rejection-handler.js

View workflow job for this annotation

GitHub Actions / Tests (16)

'RejectionStream' is not defined

Check failure on line 154 in lib/winston/rejection-handler.js

View workflow job for this annotation

GitHub Actions / Tests (18)

'RejectionStream' is not defined

Check failure on line 154 in lib/winston/rejection-handler.js

View workflow job for this annotation

GitHub Actions / Tests (20)

'RejectionStream' is not defined
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
Loading