Error management

Route's internal middleware queue execution works with Promises (bluebirdjs).


//...
const route = router.buildRoute({/*options*/});

route.main(function(req, res) {
    return asyncProcedure();
}).catch(TypeError, function(err) {
    err.message === 'error message'; //true
});

function asyncProcedure() {
    return Promise.reject(new TypeError('error message'));
}

Service App can respond to a request only with an error of type RequestError thus all other errors which don't extend the RequestError cause internal service error. All unexpected errors - aka. all errors which do NOT inherit RequestError or are instance of ServiceError - are logged to configured destination (file system by default) by the serviser-logger package.
See serviser ErrorList for the list of built-in Error types.

const service = require('serviser');
var RequestError = service.error.RequestError;

route.main(function(req, res) {
    throw new RequestError({message: 'User not found'});
});

You can influence the way errors are processed by the following events:

Next: input data Validation tutorial.

Internal guts notes:
All errors which happen in a request lifecycle get eventually processed by internal errorHandler middleware.

comments powered by Disqus