Building complex async applications is really hard. Whether you use callbacks, Promises, or EventEmitters, Error objects should have a place in your utility belt. They are indispensable when it comes to managing work flows in a highly asynchronous environment.
This talk covers patterns for using JavaScript Error (with a capital E) objects to build resilient applications, and introduce some modules that can be used to build errors with an elegant history of stack traces even through multiple asynchronous operations. Try/catch, callbacks, and other error handling mechanisms will be examined, revealing some potential deficiencies in the JavaScript language for dealing with errors.
Video: https://www.youtube.com/watch?v=PyCHbi_EqPs
20. You could throw anything
> throw { foo: bar };
> throw 0;
> throw 'my error';
21.
22. > throw 0;
undefined
> throw 'something happened';
something happened
> throw new Error('something happened');
Error: something happened
at repl:1:7
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
53. Can you recover?
function dispatch(doThings) {
if (!Array.isArray(doThings)) {
throw new TypeError('not an array');
}
for (var i = 0; i < doThings.length; i++) {
if (typeof doThings[i] !== 'function') {
throw new TypeError('not a function');
}
}
for (var i = 0; i < doThings.length; i++) {
doThings[i]();
}
}
54. Can you recover?
function dispatch(doThings) {
if (!Array.isArray(doThings)) {
throw new TypeError('not an array');
}
for (var i = 0; i < doThings.length; i++) {
if (typeof doThings[i] !== 'function') {
throw new TypeError('not a function');
}
}
for (var i = 0; i < doThings.length; i++) {
doThings[i]();
}
}
<----------------------------------- (1)
55. Can you recover?
function dispatch(doThings) {
if (!Array.isArray(doThings)) {
throw new TypeError('not an array');
}
for (var i = 0; i < doThings.length; i++) {
if (typeof doThings[i] !== 'function') {
throw new TypeError('not a function');
}
}
for (var i = 0; i < doThings.length; i++) {
doThings[i]();
}
}
<----------------------------------- (1)
<------------------------------- (2)
56. Can you recover?
function dispatch(doThings) {
if (!Array.isArray(doThings)) {
throw new TypeError('not an array');
}
for (var i = 0; i < doThings.length; i++) {
if (typeof doThings[i] !== 'function') {
throw new TypeError('not a function');
}
}
for (var i = 0; i < doThings.length; i++) {
doThings[i]();
}
}
<----------------------------------- (1)
<------------------------------- (2)
<-------------------------------------------------------- (3)
57. Catch and do what…?
for (var i = 0; i < doThings.length; i++) {
try {
doThings[i]();
} catch (e) {
...
}
}
58. Catch and do what…?
for (var i = 0; i < doThings.length; i++) {
try {
doThings[i]();
} catch (e) {
...
}
}
<--------------------- what goes here?
80. We can do better
database.get('iliad', function(err, data) {
if (err) {
err.cause = 'db error!';
err.timestamp = new Date();
return cb(err);
}
return cb(null, data);
});
81. VError: we can do even better
database.get('iliad', function(err, data) {
if (err) {
return cb(new VError(err, 'db error!'));
}
return cb(null, data);
});
82. VError: we can do even better
// redis error
[Error: missing key]
// our verror
{ [VError: db error: missing key]
jse_shortmsg: 'db error',
jse_summary: 'db error: missing key',
jse_cause: [Error: missing key],
message: 'db error: missing key' }
83. [2015-10-02T06:41:54.389Z] ERROR: test/11497 on lgml-aliu: db error: missing key
VError: db error: missing key
at Object.<anonymous> (/Users/aliu/Desktop/test/a.js:6:12)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
Caused by: Error: missing key
at Object.<anonymous> (/Users/aliu/Desktop/test/a.js:5:12)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
84. restify-errors: the bees’ knees
var errs = require('restify-errors');
errs.makeConstructor('DatabaseError', {
fooProp: 'someVal'
});
var myErr = new errs.DatabaseError('db error!');
// myErr.fooProp => someVal
85. restify-errors: the bees’ knees
var myErr = new errs.DatabaseError('db error!');
myErr instanceof errs.DatabaseError // => true
myErr instanceof VError // => true
myErr instanceof Error // => true
86. Serving out the UI
request('/api/story', function(err, fpStoryData) {
if (err instanceof HttpError) {
return res.render('NetworkErrorPage');
} else if (err instanceof DatabaseError) {
return res.render('UnavailablePage');
} else {
return res.render('ErrorPage');
}
res.render('iliad', fpStoryData);
});
87. [2015-10-06T17:50:31.561Z] ERROR: test/64987 on lgml-aliu: rendering error!
RenderError: rendering error page!
at Object.<anonymous> (/Users/aliu/Desktop/test/test4.js:10:10)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Caused by: DataValidationError: data validation error
at Object.<anonymous> (/Users/aliu/Desktop/test/test4.js:10:32)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Caused by: RequestError: request error
at Object.<anonymous> (/Users/aliu/Desktop/test/test4.js:10:63)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Caused by: BadRequestError: http 400
at Object.<anonymous> (/Users/aliu/Desktop/test/test4.js:10:86)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
88. [2015-10-06] ERROR: test/64987 on lgml-aliu: rendering error!
Error: something happened
at repl:1:7
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
104. $ node promiseDemo.js
Unhandled rejection ReferenceError: y is not defined
at foo (/test/promiseDemo.js:14:13)
at /test/promiseDemo.js:8:3
at processImmediate [as _immediateCallback] (timers.js:368:17)
From previous event:
at Object.<anonymous> (/test/promiseDemo.js:7:3)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3
114. Making Catch Better
You don't need to sell me
-- we had } catch (e if ...) {
in SpiderMonkey and
proposed it for ES3 (in
1998).
- Brendan Eich
try {
...
} catch (e if ...) {
...
}