Coming to EnterpriseJS:

In a highly asynchronous environment like Node, it is common to make requests for multiple external resources for each incoming request. Node excels in this kind of environment. To deal with this complexity we use modules and libraries that help us more easily manage asynchronous workflows. However, asynchronous workflows are just as susceptible to errors as their synchronous counterparts.

Unlike synchronous code, we cannot just throw on encountering an error. Anyone who has written JavaScript has likely tried this before:

// Imagine a simple middleware that fetches the NFLX stock price
function (req, res, next) {  
  try {
    getStockPrice("NFLX", function (price) {
      req.stockPrice = price;
  } catch(err) {
    console.log("Couldn't get stock price!");

But we know you can’t try/catch asynchronous workflows, so error handling often falls by the wayside and quickly becomes an afterthought. It is common to either see code failing silently or swallowing errors silently. This treats errors as an exceptional scenario (e.g. “that’s a corner case”).

The reality is that errors are inevitable, with the vast majority of them being errors that we expect to occur as part of the normal operating procedure. We can and should handle them in a way that doesn’t impact the user experience. This includes anything from network errors to data errors; even auth issues by sending users to a /login page if necessary.

Few applications are as simple as this example though. Most Node apps have multiple operations happening in parallel where failures in some operations are acceptable, but failures in others are not. In these scenarios, it’s incredibly useful to be able to build contextualized Error Objects that can maintain relevant context throughout the lifetime of the request. We can then inspect these combined Error Objects at the very end, and decide how the request should be handled based on the Error Context.

I’ll be talking more about Error Objects and related topics at the upcoming EnterpriseJS Event:
Tuesday, October 27th I 5:30-9PM
@ Intuit 2570 Coast Ave, Building 6
Mountain View, CA 94043