Errors
Errors assigned to BeyondAssets servers responses.
This module allows servers reuse errors, acheiving more comprehensive and readable error responses,
and more importantly - enables better logging and tracing of errors by client applications or error analysis thanks to well-defined error codes.
Usage
Installation
$ npm install beyondasset-errors
Creating error messages
First, require
the module like so:
Create specific Error instance of one of the supported error classes:
console
gives us:
{ code: 10001,
status: 422,
name: 'InvalidAddressError',
message: 'Invalid address' }
Override messages on instantiation:
console
outputs:
{ explanation: 'Address 123xyz is not a valid address',
response: 'Change the argument \'fromAddress\' to be a valid address',
code: 10001,
status: 422,
name: 'InvalidAddressError',
message: 'Invalid address' }
Express Middleware
Express.js error handling middleware.
var errorhandler =
errorhandler(options)
Create new error handling middleware.
options
env
'development' will include stack trace, and will accumulate original errors
initiated from third party or separate servers. Default is undefined.
log
One of two types:
boolean - a boolean for determining whether the error handler should log the error messages. true
will use console.error
by default for logging.
function - a function to process an error, invoked with err
.
Default is true
.
Example
As with any express error handling middleware, it should be put after the router middleware:
var express = var app = var bodyParser = var errorHandler = errorHandler appappapp
Development
Defining error messages
Create a very barebones error - you must specify at least the error name
and code
.
errors;console;
outputs:
{ code: 20001,
name: 'RuntimeError'}
Optionally, define default message, associated HTTP status code, explanation and response:
// default status, explanation and response errors;console;
gives us:
{ explanation: 'The file /home/boden/foo could not be found',
response: 'Verify the file exists and retry the operation',
code: 30001,
status: 500,
name: 'FileNotFoundError',
message: 'The requested file could not be found' }
Running the tests
$ npm install
$ mocha