- Default behavior for programs is to terminate on an error, or enter the debugger
- Terrible user experience
- Gracefully Handle Errors (retry connections, re-prompt for user input, etc)
- Non Local Control Flow (Display error messages and screens)
- “Ooops, something went wrong"
- callback(“Oops something went wrong”);
function myFunction (callback) {
doSomethingAsync(function () {
// …
if (somethingWrong) {
callback('This is my error')
} else {
callback(null, …);
}
});
}
- new Error(“Oops something went wrong”);
- callback(new Error(“Oops something went wrong”);
function myFunction (callback) {
doSomethingAsync(function () {
// …
if (somethingWrong) {
callback(new Error('This is my error'))
} else {
callback(null, …);
}
});
}
- see where the error happened in your code with line numbers
- OMG STACK TRACES
// error.js
var err = new Error();
console.log(typeof err.stack);
console.log(err.stack);
∞ node error.js
string
Error
at Object.<anonymous> (/private/tmp/error.js:2:11)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array.0 (module.js:430:10)
at EventEmitter._tickCallback (node.js:126:26)
- Define your own error types
- Handle with instanceof
function MongooseError (msg) {
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
this.message = msg;
this.name = 'MongooseError';
};
MongooseError.prototype.__proto__ = Error.prototype;
function NotFound(msg) {
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
this.name = 'NotFound';
this.message = msg;
}
NotFound.prototype.__proto__ = Error.prototype;
exports.NotFound = NotFound;
function errorHandler (err, req, res, next) {
if (err instanceof NotFound) {
res.status(404);
return res.render('errors/404', {
config: config,
url: req.url,
identifier: 'error'
});
}
res.status(500);
return res.render('errors/500', {
config: config,
url: req.url,
identifier: 'error'
});
http://www.devthought.com/2011/12/22/a-string-is-not-an-error/ http://eloquentjavascript.net/1st_edition/chapter5.html
Trevor,
Your code was 99.999% of the way there. You were just missing the "new" keyword between "throw" and "Testing Error".
I threw the code into a fiddle and changed console to alert so the result is more obvious. I also tweaked the error handler a little but the "new" keyword is the only thing that was causing your code not to execute as you expected.
http://jsfiddle.net/s846na6z/