This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An error logger backed by Loggly | |
*/ | |
require('dotenv').config({silent: false}); | |
var loggly = require('loggly'); | |
var client = loggly.createClient({ | |
token: process.env.LOGGLY_TOKEN, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const VError = require('verror').VError; | |
const WError = require('verror').WError; | |
const errReqFail = new Error('Request failed'); | |
const errReqStatus = new VError(errReqFail, 'Unexpected status code "%s"', '500'); | |
const errReq = new WError(errReqStatus, 'Internal error'); | |
console.error(errReq.message); //Internal error: | |
// get some real data for logging | |
console.info(errReq.toString()); //WError: Internal error; caused by VError: Unexpected status code "500": Request failed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const VError = require('verror'); | |
const errReqFail = new Error('Request failed'); | |
const errReqStatus = new VError(errReqFail, 'Unexpected status code "%s"', '500'); | |
const errReqBase = new VError(errReqStatus, 'Internal error'); | |
console.error(errReqBase); //VError: Internal error: Unexpected status code "500": Request failed | |
// unwind to check cause | |
console.info(errReqBase.cause().message); //Unexpected status code "500": Request failed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function * genThrow() { | |
yield 'egg'; | |
throw new Error('I am out of eggs'); | |
} | |
try { | |
const eggy = genThrow(); | |
eggy.next(); | |
// second call invokes throw | |
eggy.next(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function *genTestError() { | |
while (true) { | |
try { | |
var val = yield null; | |
console.log('value', val); | |
} catch (e) { | |
console.log('There was an error', e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('uncaughtException', (err) => { | |
logger.log('whoops! There was an uncaught error', err); | |
// do a graceful shutdown, | |
// close the database connection etc. | |
process.exit(1); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventEmitter = require('events'); | |
class Emitter extends EventEmitter {} | |
const emitter = new Emitter(); | |
const logger = console; | |
/** | |
* Add Error listener | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('unhandledRejection', function (reason, promise) { | |
logger.error('Unhandled rejection', {reason: reason, promise: promise}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A custom MyError class | |
* @class | |
*/ | |
class MyError extends Error { | |
/** | |
* Constructs the MyError class | |
* @param {String} message an error message | |
* @constructor | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A custom MyError class | |
* @class | |
*/ | |
class MyError extends Error { | |
/** | |
* Constructs the MyError class | |
* @param {String} message an error message | |
* @constructor | |
*/ |