page.js 3.7 KB

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