Errors emitted with BeyondAsset server responses.
Last updated 7 months ago by tech1k .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install beyondasset-errors 
SYNC missed versions from official npm registry.

Build Status Coverage Status npm version


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.



$ 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.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'


{ 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('beyondasset-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('beyondasset-errors').errorHandler

app.get('/error', function (req, res, next) {
  next('Something went wrong')


Defining error messages

Create a very barebones error - you must specify at least the error name and code.

  name: 'RuntimeError', // class name
  code: 20001, // error code
console.log(new errors.RuntimeError().toJSON());


{ code: 20001,
  name: 'RuntimeError'}

Optionally, define default message, associated HTTP status code, explanation and response:

// default status, explanation and response 
    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

Current Tags

  • 0.1.4                                ...           latest (7 months ago)

1 Versions

  • 0.1.4                                ...           7 months ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
  • debug ^2.2.0
  • errors git+
  • lodash ^4.14.0
Dev Dependencies (9)

Copyright 2014 - 2016 © |