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
var CodeError = require('node-code-error')
CodeError.extend('api', 400, 100)
var err = CodeError('api', 'db is not defined', 32)
err.status
err.toString()
err.toJSON()
CodeError.configure({
maps: [{
user: 1,
book: 2,
cat: 3
}, {
missed: 1,
invalid: 2,
unmatched: 3
}],
splitLetter: ' '
useMsgForCode: true
})
err = CodeError('api', 'user invalid')
err.status
err.toString()
err.toJSON()
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')
CodeError('api', 'api error', 32, new Error('xxx'))
CodeError.extend(name, status, baseCode, customCode)
name
Required, String
, define the type of the errorstatus
Number
, define the status of the error, default is 500baseCode
Required, Number
, the first part of the codecustomCode
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)
CodeError.extend('system', 500, 101, 10)
CodeError('system', 'system error', 43)
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: ' '
useMsgForCode: true
})
CodeError.extend('api', 400, 100)
var err = CodeError('api', 'use missed')
err.code
var err = CodeError('api', 'book unmatch')
err.code
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()
wrapped.toJSON()