sequence.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const npm = {
  2. u: require('util'),
  3. os: require('os'),
  4. utils: require('../utils/static')
  5. };
  6. const errorReasons = {
  7. 0: 'Source %s returned a rejection at index %d.',
  8. 1: 'Source %s threw an error at index %d.',
  9. 2: 'Destination %s returned a rejection at index %d.',
  10. 3: 'Destination %s threw an error at index %d.'
  11. };
  12. /**
  13. * @class errors.SequenceError
  14. * @augments external:Error
  15. * @description
  16. * This type represents all errors rejected by method {@link sequence}, except for {@link external:TypeError TypeError}
  17. * when the method receives invalid input parameters.
  18. *
  19. * @property {string} name
  20. * Standard {@link external:Error Error} property - error type name = `SequenceError`.
  21. *
  22. * @property {string} message
  23. * Standard {@link external:Error Error} property - the error message.
  24. *
  25. * @property {string} stack
  26. * Standard {@link external:Error Error} property - the stack trace.
  27. *
  28. * @property {} error
  29. * The error that was thrown or the rejection reason.
  30. *
  31. * @property {number} index
  32. * Index of the element in the sequence for which the error/rejection occurred.
  33. *
  34. * @property {number} duration
  35. * Duration (in milliseconds) of processing until the error/rejection occurred.
  36. *
  37. * @property {string} reason
  38. * Textual explanation of why the method failed.
  39. *
  40. * @property {} source
  41. * Resolved `data` parameter that was passed into the `source` function.
  42. *
  43. * It is only set when the error/rejection occurred inside the `source` function.
  44. *
  45. * @property {} dest
  46. * Resolved `data` parameter that was passed into the `dest` function.
  47. *
  48. * It is only set when the error/rejection occurred inside the `dest` function.
  49. *
  50. * @see {@link sequence}
  51. *
  52. */
  53. class SequenceError extends Error {
  54. constructor(e, code, cbName, duration) {
  55. let message;
  56. if (e.error instanceof Error) {
  57. message = e.error.message;
  58. } else {
  59. message = e.error;
  60. if (typeof message !== 'string') {
  61. message = npm.u.inspect(message);
  62. }
  63. }
  64. super(message);
  65. this.name = this.constructor.name;
  66. this.index = e.index;
  67. this.duration = duration;
  68. this.error = e.error;
  69. if ('source' in e) {
  70. this.source = e.source;
  71. } else {
  72. this.dest = e.dest;
  73. }
  74. cbName = cbName ? ('\'' + cbName + '\'') : '<anonymous>';
  75. this.reason = npm.u.format(errorReasons[code], cbName, e.index);
  76. Error.captureStackTrace(this, this.constructor);
  77. }
  78. }
  79. /**
  80. * @method errors.SequenceError.toString
  81. * @description
  82. * Creates a well-formatted multi-line string that represents the error.
  83. *
  84. * It is called automatically when writing the object into the console.
  85. *
  86. * @param {number} [level=0]
  87. * Nested output level, to provide visual offset.
  88. *
  89. * @returns {string}
  90. */
  91. SequenceError.prototype.toString = function (level) {
  92. level = level > 0 ? parseInt(level) : 0;
  93. const gap0 = npm.utils.messageGap(level),
  94. gap1 = npm.utils.messageGap(level + 1),
  95. lines = [
  96. 'SequenceError {',
  97. gap1 + 'message: ' + JSON.stringify(this.message),
  98. gap1 + 'reason: ' + this.reason,
  99. gap1 + 'index: ' + this.index,
  100. gap1 + 'duration: ' + this.duration
  101. ];
  102. lines.push(gap1 + 'error: ' + npm.utils.formatError(this.error, level + 1));
  103. lines.push(gap0 + '}');
  104. return lines.join(npm.os.EOL);
  105. };
  106. npm.utils.addInspection(SequenceError, function () {
  107. return this.toString();
  108. });
  109. module.exports = {SequenceError};