error3
Version:
Error3 is proper JS error implementation. It supports error codes, message formatting (e.g. i18n) and nested errors.
1 lines • 1.67 kB
JavaScript
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?module.exports=b():"function"==typeof define&&define.amd?define(b):(a=a||self,a.Error3=b())})(this,function(){'use strict';function a(a){return Array.isArray(a)&&a.every(function(a){return c(a)})}function b(a){return null!==a&&"object"==typeof a}function c(a){return b(a)&&a instanceof Error}function d(a){return a.constructor.toString()===Object.toString()}var e=function(){var a=function(c,d){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(a,c){a.__proto__=c}||function(a,c){for(var b in c)c.hasOwnProperty(b)&&(a[b]=c[b])},a(c,d)};return function(c,d){function b(){this.constructor=c}a(c,d),c.prototype=null===d?Object.create(d):(b.prototype=d.prototype,new b)}}(),f=function(){return f=Object.assign||function(a){for(var b,c=1,d=arguments.length;c<d;c++)for(var e in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,e)&&(a[e]=b[e]);return a},f.apply(this,arguments)},g=function(c){function g(e,g){var h=c.call(this)||this;if(h.code=0,h.name=h.constructor.name,!e)h.details={};else if(b(e)&&d(e))h.details=f({},e);else throw new Error("Details should be a plain Object instance or undefined");if(!g)h.errors=[];else if(a(g))h.errors=g;else throw new Error("Errors should be an array of errors");return h.message=h.format(e,g),h}return e(g,c),g.prototype.valueOf=function(){return{code:this.code,message:this.message,details:this.details,errors:this.errors.map(function(a){return a.valueOf()})}},g.prototype.toString=function(){var a=this,b=a.name,c=a.code,d=a.message;return b+": [#"+c+"] "+d},g.prototype.toJSON=function(){return this.valueOf()},g}(Error);return g});