Error request handler
error request handler is module based on Request module, this module allows you to use the power of Request with a simple to use error handling machenisim.
You can return custom errors based on api error code using your own custom error/function.
How?
npm install error-request-handler
var errorHandlerRequest = ; ; })
more
The request function takes 4 arguments.
uri
the uri of the api resource.
options
specific request options ex. method type, body, headers... *can take every option that request module accepts.
errorObject
this object contains key value pair of error number/string and error handler (custom error/function). it's possible to specify '*' error handler for each unspecified error, this handler will catch every error if a specific error handler wasn't provided.
callback
this function take (err, response, body) and allows you to handle the response.
The flow
When a response return from the server it goes through generalErrorHandler (specified in options), your custom errorObject and then your custom callback. generalErrorHandler check if an general error occurred and return next with or without an error. After the general error handler the specific error handler will look for the error in the error object* and then run the corresponding error function or throw the custom error provided. your callback function fired at the end and get the error object* ,response and body.
*if occurred
Your custom request function
You can create custom base request that you can use across your entire application by doing this:
var errorHandlerRequest = ;var customRequest = errorHandlerRequest;;
Options
default options object
var defaults = requestOptions: headers: "Accept": "JSON" "Content-Type": "application/json" json: true errorCodeField: "errorCode" { ; };
requestOptions
you can specify default request object. !important by default the request options are set to allow json response in order to handle the errors.
errorCodeField
this option specify the name of the error code property in the body object.
body = errorCode:15633 moreData: "from server" ...;
generalErrorHandler
This function take 3 arguments (response, body, next). In this function you can check for a general errors like the ones in the response.statusCode, this function triggered before the custom error handler. by default this function returned next. !important this function should always return next at each endpoint of your code.