Version 1
[yaffs-website] / node_modules / bluebird / js / release / errors.js
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     notEnumerableProp(Error, "__BluebirdErrorTypes__", errorTypes);
100 }
101
102 module.exports = {
103     Error: Error,
104     TypeError: _TypeError,
105     RangeError: _RangeError,
106     CancellationError: errorTypes.CancellationError,
107     OperationalError: errorTypes.OperationalError,
108     TimeoutError: errorTypes.TimeoutError,
109     AggregateError: errorTypes.AggregateError,
110     Warning: Warning
111 };