errors.js 3.5 KB

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