UNPKG

error3

Version:

Error3 is proper JS error implementation. It supports error codes, message formatting (e.g. i18n) and nested errors.

1 lines 923 B
export default class Error3 extends Error{constructor(a,b){if(super(),this.code=0,this.name=this.constructor.name,!a)this.details={};else if(isObject(a)&&isPlainObject(a))this.details=Object.assign({},a);else throw new Error("Details should be a plain Object instance or undefined");if(!b)this.errors=[];else if(isErrors(b))this.errors=b;else throw new Error("Errors should be an array of errors");this.message=this.format(a,b)}valueOf(){return{code:this.code,message:this.message,details:this.details,errors:this.errors.map(a=>a.valueOf())}}toString(){const{name:a,code:b,message:c}=this;return`${a}: [#${b}] ${c}`}toJSON(){return this.valueOf()}}function isErrors(a){return Array.isArray(a)&&a.every(a=>isErrorObject(a))}function isObject(a){return null!==a&&"object"==typeof a}function isErrorObject(a){return isObject(a)&&a instanceof Error}function isPlainObject(a){return a.constructor.toString()===Object.toString()}