digiasset-errors
v0.1.4
Published
Errors emitted with DigiAsset server responses.
Downloads
4
Readme
Errors
Errors assigned to Colored-Coins 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 digiasset-errors
Creating error messages
First, require
the module like so:
require('digiasset-errors')
Create specific Error instance of one of the supported error classes:
console.log(new errors.InvalidAddressError().toJSON())
gives us:
{ code: 10001,
status: 422,
name: 'InvalidAddressError',
message: 'Invalid address' }
Override messages on instantiation:
console.log(new errors.InvalidAddressError({
explanation: 'Address 123xyz is not a valid address',
response: 'Change the argument \'fromAddress\' to be a valid address'
}).toJSON())
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 = require('digiasset-errors').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 = require('express')
var app = express()
var bodyParser = require('body-parser')
var errorHandler = require('digiasset-errors').errorHandler
app.use(bodyParser())
app.get('/error', function (req, res, next) {
next('Something went wrong')
})
app.use(errorHandler())
Development
Defining error messages
Create a very barebones error - you must specify at least the error name
and code
.
errors.create({
name: 'RuntimeError', // class name
code: 20001, // error code
});
console.log(new errors.RuntimeError().toJSON());
outputs:
{ code: 20001,
name: 'RuntimeError'}
Optionally, define default message, associated HTTP status code, explanation and response:
// default status, explanation and response
errors.create({
name: 'FileNotFoundError',
code: 30001,
status: 500, // associated HTTP status
defaultMessage: 'The requested file could not be found', // human readable, short and precise string
defaultExplanation: 'The file /home/boden/foo could not be found', // detailed information
defaultResponse: 'Verify the file exists and retry the operation' // suggested action to user
});
console.log(new errors.FileNotFoundError().toJSON());
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