• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2var es5 = require("./es5");
3var Objectfreeze = es5.freeze;
4var util = require("./util");
5var inherits = util.inherits;
6var notEnumerableProp = util.notEnumerableProp;
7
8function 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
24var _TypeError, _RangeError;
25var Warning = subError("Warning", "warning");
26var CancellationError = subError("CancellationError", "cancellation error");
27var TimeoutError = subError("TimeoutError", "timeout error");
28var AggregateError = subError("AggregateError", "aggregate error");
29try {
30    _TypeError = TypeError;
31    _RangeError = RangeError;
32} catch(e) {
33    _TypeError = subError("TypeError", "type error");
34    _RangeError = subError("RangeError", "range error");
35}
36
37var methods = ("join pop push shift unshift slice filter forEach some " +
38    "every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" ");
39
40for (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
46es5.defineProperty(AggregateError.prototype, "length", {
47    value: 0,
48    configurable: false,
49    writable: true,
50    enumerable: true
51});
52AggregateError.prototype["isOperational"] = true;
53var level = 0;
54AggregateError.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
72function 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}
88inherits(OperationalError, Error);
89
90var errorTypes = Error["__BluebirdErrorTypes__"];
91if (!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
107module.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};
117