You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.6 KiB

4 years ago
  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. 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. es5.defineProperty(Error, "__BluebirdErrorTypes__", {
  91. value: errorTypes,
  92. writable: false,
  93. enumerable: false,
  94. configurable: false
  95. });
  96. }
  97. module.exports = {
  98. Error: Error,
  99. TypeError: _TypeError,
  100. RangeError: _RangeError,
  101. CancellationError: errorTypes.CancellationError,
  102. OperationalError: errorTypes.OperationalError,
  103. TimeoutError: errorTypes.TimeoutError,
  104. AggregateError: errorTypes.AggregateError,
  105. Warning: Warning
  106. };