npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

digiasset-errors

v0.1.4

Published

Errors emitted with DigiAsset server responses.

Downloads

4

Readme

Build Status Coverage Status npm version

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