Skip to content

Instantly share code, notes, and snippets.

@Tiriel
Last active August 28, 2017 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiriel/95278e427d7317224b304231f84eee31 to your computer and use it in GitHub Desktop.
Save Tiriel/95278e427d7317224b304231f84eee31 to your computer and use it in GitHub Desktop.
NodeJS utilities (outgoing requests interceptor, unhandledRejection monitor)
'use-strict';
/**
* Debug toolbox startng point
* Usage :
* const utilities = require('path-to-utilities.js');
* utilities.requestLogger(require('http'));
* utilities.unhandledRejectionMonitor();
*/
module.exports.requestLogger = function(httpModule){
let original = httpModule.request;
httpModule.request = function(options, callback){
console.log('----- Request :----');
console.log('-- URL : '+options.href||'URL : '+options.proto+"://"+options.host+options.path);
console.log('-- Method : '+options.method);
console.log('-- Headers : '+options.headers);
console.log('-- Body : '+options.body);
console.log('-------------------');
return original(options, callback)
}
}
module.exports.unhandledRejectionsMonitor = function () {
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
console.log('----- UnhandledRejection :----');
console.log('-- Object : ' + p);
console.log('-- Reason : ' + reason);
console.log('-- source : ' + reason.fileName + ' - ' + reason.lineNumber);
console.log('------------------------------');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment