errors.js (3717B)
1 "use strict"; 2 var es5 = require("./es5"); 3 var Objectfreeze = es5.freeze; 4 var util = require("./util"); 5 var inherits = util.inherits; 6 var notEnumerableProp = util.notEnumerableProp; 7 8 function subError(nameProperty, defaultMessage) { 9 function SubError(message) { 10 if (!(this instanceof SubError)) return new SubError(message); 11 notEnumerableProp(this, "message", 12 typeof message === "string" ? message : defaultMessage); 13 notEnumerableProp(this, "name", nameProperty); 14 if (Error.captureStackTrace) { 15 Error.captureStackTrace(this, this.constructor); 16 } else { 17 Error.call(this); 18 } 19 } 20 inherits(SubError, Error); 21 return SubError; 22 } 23 24 var _TypeError, _RangeError; 25 var Warning = subError("Warning", "warning"); 26 var CancellationError = subError("CancellationError", "cancellation error"); 27 var TimeoutError = subError("TimeoutError", "timeout error"); 28 var AggregateError = subError("AggregateError", "aggregate error"); 29 try { 30 _TypeError = TypeError; 31 _RangeError = RangeError; 32 } catch(e) { 33 _TypeError = subError("TypeError", "type error"); 34 _RangeError = subError("RangeError", "range error"); 35 } 36 37 var methods = ("join pop push shift unshift slice filter forEach some " + 38 "every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" "); 39 40 for (var i = 0; i < methods.length; ++i) { 41 if (typeof Array.prototype[methods[i]] === "function") { 42 AggregateError.prototype[methods[i]] = Array.prototype[methods[i]]; 43 } 44 } 45 46 es5.defineProperty(AggregateError.prototype, "length", { 47 value: 0, 48 configurable: false, 49 writable: true, 50 enumerable: true 51 }); 52 AggregateError.prototype["isOperational"] = true; 53 var level = 0; 54 AggregateError.prototype.toString = function() { 55 var indent = Array(level * 4 + 1).join(" "); 56 var ret = "\n" + indent + "AggregateError of:" + "\n"; 57 level++; 58 indent = Array(level * 4 + 1).join(" "); 59 for (var i = 0; i < this.length; ++i) { 60 var str = this[i] === this ? "[Circular AggregateError]" : this[i] + ""; 61 var lines = str.split("\n"); 62 for (var j = 0; j < lines.length; ++j) { 63 lines[j] = indent + lines[j]; 64 } 65 str = lines.join("\n"); 66 ret += str + "\n"; 67 } 68 level--; 69 return ret; 70 }; 71 72 function OperationalError(message) { 73 if (!(this instanceof OperationalError)) 74 return new OperationalError(message); 75 notEnumerableProp(this, "name", "OperationalError"); 76 notEnumerableProp(this, "message", message); 77 this.cause = message; 78 this["isOperational"] = true; 79 80 if (message instanceof Error) { 81 notEnumerableProp(this, "message", message.message); 82 notEnumerableProp(this, "stack", message.stack); 83 } else if (Error.captureStackTrace) { 84 Error.captureStackTrace(this, this.constructor); 85 } 86 87 } 88 inherits(OperationalError, Error); 89 90 var errorTypes = Error["__BluebirdErrorTypes__"]; 91 if (!errorTypes) { 92 errorTypes = Objectfreeze({ 93 CancellationError: CancellationError, 94 TimeoutError: TimeoutError, 95 OperationalError: OperationalError, 96 RejectionError: OperationalError, 97 AggregateError: AggregateError 98 }); 99 es5.defineProperty(Error, "__BluebirdErrorTypes__", { 100 value: errorTypes, 101 writable: false, 102 enumerable: false, 103 configurable: false 104 }); 105 } 106 107 module.exports = { 108 Error: Error, 109 TypeError: _TypeError, 110 RangeError: _RangeError, 111 CancellationError: errorTypes.CancellationError, 112 OperationalError: errorTypes.OperationalError, 113 TimeoutError: errorTypes.TimeoutError, 114 AggregateError: errorTypes.AggregateError, 115 Warning: Warning 116 };