title: Error handling in an Express route
body: <p>I have a Node module that wraps a RESTful API. This client follows the standard Node callback pattern:</p>
  
  <pre><code>module.exports = { 
      GetCustomer = function(id, callback) { ...} 
  }
  </code></pre>
  
  <p>I am calling this client from various Express routes like so:</p>
  
  <pre><code>app.get('/customer/:customerId', function(req,res) {
   MyClient.GetCustomer(customerId, function(err,data) {
     if(err === "ConnectionError") {
       res.send(503);
      }
     if(err === "Unauthorized") {
       res.send(401);
      }
      else {
       res.json(200, data);
      }
    };
  };
  </code></pre>
  
  <p>The issue is that I think it's not DRY to check for "ConnectionError" every time I call this client. I don't believe I can call <code>res.next(err)</code> because that will get sent back as a 500 error.</p>
  
  <p>Is there a Node or Javascript pattern I am missing here? In C# or Java, I would throw the appropriate exception in MyClient.</p>