UNPKG

z-error

Version:

* verify: 校验字段有效性,合法性。检验不通过返回ZError对象,否则返回null * ZError: 错误提示,继承Error。name表示错误名称,code表示错误码,message表示code对应的提示信息,description错误描述。status表示restful api 状态码 * setLocal:设置多语言错误对照码

41 lines (34 loc) 928 B
'use strict'; const getMessage = require('./util').getMessage; const config = require('./config'); class ZError extends Error { constructor(name = '', code = '', message = '', description = '', status = 200, lang = 'en'){ super(message); Error.captureStackTrace(this, this.constructor); this.name = name; this.code = code; this.lang = lang; this.message = message || (code ? getMessage(code, config.lang || lang) : '') || code; this.description = description; this.status = status; } getMessage(lang){ if(!this.code) return ''; let tmpLang = lang || this.lang || config.lang; return getMessage(this.code, tmpLang); } toJson() { return { name: this.name, code: this.code, message: this.message, description: this.description, status: this.status, } } toJSON() { return this.toJson(); } } module.exports = ZError;