Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@springworks/error-factory

Package Overview
Dependencies
Maintainers
1
Versions
434
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@springworks/error-factory

Simple Node.js module to provide better error handling throughout an application.

  • 1.2.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
510
decreased by-42.57%
Maintainers
1
Weekly downloads
 
Created
Source

Node.js Error module

Simple Node.js module to provide better error handling throughout an application.

Makes use of HTTP error codes to identify different error types.

Build Status Coverage Status

Install

npm i -S @springworks/error-factory

API:

var ErrorFactory = require('error-factory');

create([code], [message], [...var_args])

Create an error with status code and message. The code is a HTTP status code and defaults to 500. Message can be any string but defaults to the name of the status code. Any extra arguments will be formated into the message.

var err = ErrorFactory.create(500, 'Unable to send email to %s', 'user@example.com');
err.code; // 400
err.statusCode; // 400 (alias of code)
err.message; // 'Invalid input parameter: key'
err.json; // { message: 'Invalid input parameter: key' }
err.toJSON(); // same as err.json

validationError([message], [...var_args])

ErrorFactory.validationError is a shortcut for ErrorFactory.create(422, 'Validation Failed'). The message can be replaced with different string.

var err = ErrorFactory.validationError();
err.code; // 422
err.message; // 'Validation Failed'

err.toJSON()

Used by the JSON.stringify method for JSON serialization.

Appending error descriptions to the error.

err.appendMissingResource(resource)

This means a resource does not exist.

The resource param is the name of the resource, e.g. 'User'.

err.appendMissingField(resource, field)

This means a required field on a resource has not been set.

The field param is the name of the field, e.g. 'email'.

err.appendMissingFields(resource, fields)

Same as appendMissingField but with an array of fields.

err.appendInvalidField(resource, field)

This means the formatting of a field is invalid. The documentation for the resource should be able to give more specific information.

err.appendResourceAlreadyExists(resource, field)

This means another resource has the same value as this field. This can happen in resources that must have some unique key.

var err = ErrorFactory.validationError();

err.appendMissingResource('Thing').
    appendMissingField('User', 'name').
    appendInvalidField('User', 'password').
    appendResourceAlreadyExists('User', 'email');

var obj = err.toJSON();
// will be:
{
  message: 'Validation Failed',
  errors: [{
    resource: 'Thing',
    field: undefined,
    code: 'missing'
  }, {
    resource: 'User',
    field: 'name',
    code: 'missing_field'
  }, {
    resource: 'User',
    field: 'password',
    code: 'invalid'
  }, {
    resource: 'User',
    field: 'email',
    code: 'already_exists'
  }]
};

Tests

Run npm test to run complete unit tests with Istanbul code coverage.

Keywords

FAQs

Package last updated on 05 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc