Created
April 5, 2013 20:59
-
-
Save machadogj/5322588 to your computer and use it in GitHub Desktop.
Sample code for understanding error handling in node.js.
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', function ( err ) { | |
console.error('An uncaughtException was found, the program will end.'); | |
//hopefully do some logging. | |
process.exit(1); | |
}); | |
try | |
{ | |
setTimeout(function () { | |
throw new Error('who will catch me?'); | |
}, 1); | |
} | |
catch (e) { | |
console.log('not me'); | |
} |
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
var util = require('util'), | |
EventEmitter = require('events').EventEmitter; | |
var MyClass = function () { | |
if (!(this instanceof MyClass)) return new MyClass(); | |
EventEmitter.call(this); | |
}; | |
util.inherits(MyClass, EventEmitter); |
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
try | |
{ | |
setTimeout(function () { | |
throw new Error('who will catch me?'); | |
}, 1); | |
} | |
catch (e) { | |
console.log('not me'); | |
} |
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
app.get('/', function ( req, res, next ) { | |
getSomething(function ( err, data ) { | |
if ( err ) { | |
var report = new Error('unable to get something in home'); | |
report.status = 500; | |
report.inner = err; | |
next(report); | |
return; | |
} | |
if ( !data ) { | |
var report = new Error('something not found'); | |
report.status = 404; | |
next(report); | |
return; | |
} | |
res.send(200, data); | |
}) | |
}); |
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
var foo = new MyClass(); | |
foo.on('error', function () { console.log('it blew')}); | |
foo.emit('error'); |
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
var foo = new MyClass(); | |
foo.emit('error'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment