Error handling

The errorResponse method provided by controller request contexts has no equivalent in router endpoints. If you want to handle specific error types with specific status codes you need to catch them explicitly, either in the route or in a middleware:

Old:

  1. ctrl.get('/puppies', function (req, res) {
  2. // Exception is thrown here
  3. })
  4. .errorResponse(TooManyPuppiesError, 400, 'Something went wrong!');

New:

  1. ctrl.get('/puppies', function (req, res) {
  2. try {
  3. // Exception is thrown here
  4. } catch (e) {
  5. if (!(e instanceof TooManyPuppiesError)) {
  6. throw e;
  7. }
  8. res.throw(400, 'Something went wrong!');
  9. }
  10. })
  11. // The "error" method merely documents the meaning
  12. // of the status code and has no other effect.
  13. .error(400, 'Thrown if there are too many puppies.');

Note that errors created with http-errors are still handled by Foxx intelligently. In fact res.throw is just a helper method for creating and throwing these errors.