title: How to use an error-handler callback in a Sails.js policy?
body: <p>After asking <a href="https://stackoverflow.com/questions/22160043/how-to-use-a-callback-array-as-the-handler-for-a-sails-js-route">this question</a>, I've found that I can add a callback array to an endpoint in a sails app like this:</p>
  
  <p>file: <code>/api/policies/somepolicy.js</code></p>
  
  <pre><code>module.exports = thisIsAnArrayOfCallbacks;
  </code></pre>
  
  <p>This works ok while each member of <code>thisIsAnArrayOfCallbacks</code> is a <code>function</code> which accepts <code>req</code>, <code>res</code>, and <code>next</code> as arguments. The controller call executes all the functions in the array and the expected result is obtained in a normal flow.</p>
  
  <p>But when using an <code>errorHandler</code> callback (like the one in <a href="https://github.com/jaredhanson/oauth2orize/blob/master/examples/express2/oauth2.js#L142" rel="nofollow noreferrer">this example</a>) which takes an additional <code>err</code> parameter, it doesn't work as expected: the express-only version <code>app.get('/path', thisIsAnArrayOfCallbacks)</code> allows the <code>errorHandler</code> to fetch the exception and report a proper response to the client, but when using the sails way, the <code>errorHandler</code> function isn't called and the exception is thrown in the response.</p>
  
  <p>How could I fetch the <code>err</code> parameter or catch the exception occurred in one of the functions of <code>thisIsAnArrayOfCallbacks</code> to send a proper response (a custom one is preferred) to the client?</p>
  
  <p>Thanks in advance.</p>