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.
$ npm install cc-errors
First, require
the module like so:
require('cc-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.js error handling middleware.
var errorhandler = require('cc-errors').errorHandler()
Create new error handling middleware.
'development' will include stack trace, and will accumulate original errors
initiated from third party or separate servers. Default is undefined.
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
.
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('cc-errors').errorHandler
app.use(bodyParser())
app.get('/error', function (req, res, next) {
next('Something went wrong')
})
app.use(errorHandler())
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' }
$ npm install
$ mocha