Handle Rejections functionality is not working
See original GitHub issuewinston
version?-
winston@2
-
winston@3
-
node -v
outputs: v10.16.0- Operating System? macOS
- Language? ES6/7
What is the problem?
In both below cases nodejs will print default unhandled rejection error
- setting
handleRejections
flag totrue
during transport creation is not working - calling logger.rejections.handle(transport) is not working
What do you expect to happen instead?
Winston should intercept unhandled rejection and log it
Other information
1st problem is due to not released change in winston-transport package: https://github.com/winstonjs/winston-transport/issues/47
2nd problem is due to typo in _addHandler
method of lib/winston/rejection-handler.js
: https://github.com/winstonjs/winston/blob/15c9653e4eeae1c76875603a722e11db9a6556bd/lib/winston/rejection-handler.js#L153
This line should be ADDED handler.handleRejections = true
There is one important thing: without handleExceptions, handleRejections won’t log anything, due to below check: https://github.com/winstonjs/winston-transport/blob/46db8f3c8cd8b106ade8d7e04a191ee388683d60/index.js#L70
Testcases
Fails
const { createLogger, transports } = require('winston');
const logger = createLogger({
transports: new transports.Console({
level: 'info',
handleRejections: true,
}),
});
logger.info('Start');
new Promise((resolve, reject) => {
process.nextTick(() => {
reject(new Error('Rejected'));
});
}).then(() => {});
Succeeds (Trick is in using Object.assign
on transport instance)
const { createLogger, transports } = require('winston');
const logger = createLogger({
transports: Object.assign(
new transports.Console({
handleExceptions: true,
}),
{
handleRejections: true,
},
),
});
logger.info('Start');
new Promise((resolve, reject) => {
process.nextTick(() => {
reject(new Error('Rejected'));
});
}).then(() => {});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:13
- Comments:10
Top GitHub Comments
Any update on this? The documentation of
winston@3
on github shows the rejections section but it is not available on the npm package page.Thanks so much!