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

node-code-error

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-code-error

define node error with code

  • 0.1.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

node-code-error

define errror with code

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]

Installation

npm install node-code-error

Demo

// extend a new type of error
var CodeError = require('node-code-error')
CodeError.extend('api', 400, 100)
var err = CodeError('api', 'db is not defined', 32)
err.status // 400
err.toString() // 'ApiError: db is not defined'
err.toJSON()
/*
  {
    code: 10032,
    message: 'ApiError: db is not defined',
    type: 'ApiError'
  }
*/

// configure CodeError and describe code by keywords
CodeError.configure({
  maps: [{
    user: 1,
    book: 2,
    cat: 3
  }, {
    missed: 1,
    invalid: 2,
    unmatched: 3
  }],
  splitLetter: ' ' // default is ' ',
  useMsgForCode: true // default is false
})
// then you can create err like this
err = CodeError('api', 'user invalid')
err.status // 400
err.toString() // 'ApiError: user invalid'
err.toJSON()
/*
  {
    code: 10012,
    message: 'ApiError: user invalid',
    type: 'ApiError'
  }
*/

API

CodeError(name, msg, code, originalError)

  • factory function for getting an error constructor or creating an instance
var CodeError = require('node-code-error')
CodeError.extend('api', 400, 100)
CodeError('api') // => function ApiError
CodeError('api', 'api error', 32, new Error('xxx')) // => instance of ApiError

CodeError.extend(name, status, baseCode, customCode)

  • name Required, String, define the type of the error
  • status Number, define the status of the error, default is 500
  • baseCode Required, Number, the first part of the code
  • customCode Option, Number, if passed, the last part of code will be always the customCode, this is used for errors with stationary code, like SystemError
var CodeError = require('node-code-error')
CodeError.extend('api', 400, 100)
CodeError('api', 'api error', 43) // code == 10043
CodeError.extend('system', 500, 101, 10)
CodeError('system', 'system error', 43) // code == 10110

CodeError.configure(opts)

  • global config.only used for transfering message to code right now.
  • maps Array map of values for keywords.for common use, you can use maps[0] as map of model, use maps[1] as map of action.then, your error message will like 'user invalid', 'password unmatch', etc.
var CodeError = require('node-code-error')
CodeError.configure({
  maps: [{
    user: 1,
    book: 2,
    cat: 3
  }, {
    missed: 1,
    invalid: 2,
    unmatch: 3
  }],
  splitLetter: ' ' // default is ' ',
  useMsgForCode: true // default is false
})
CodeError.extend('api', 400, 100)
var err = CodeError('api', 'use missed')
err.code // => 10011
var err = CodeError('api', 'book unmatch')
err.code // => 10023

CodeError.wrap(err, name)

  • err Required Error the error to be wrapped
  • name Option String which type of error to wrap, default is 'system'
CodeError.extend('system', 500, 100, 10)
var err = new Error("I'm a nodejs Error")
var wrapped = CodeError.wrap(err, 'system')
wrapped.toString() // => "SystemError: I'm a nodejs Error"
wrapped.toJSON()
/*
  => {
    code: 10010,
    message: "SystemError: I'm a nodejs Error",
    type: "SystemError"
  }
*/

Keywords

FAQs

Package last updated on 30 Mar 2016

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