index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict'
  2. var __extends =
  3. (this && this.__extends) ||
  4. (function () {
  5. var extendStatics = function (d, b) {
  6. extendStatics =
  7. Object.setPrototypeOf ||
  8. ({ __proto__: [] } instanceof Array &&
  9. function (d, b) {
  10. d.__proto__ = b
  11. }) ||
  12. function (d, b) {
  13. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]
  14. }
  15. return extendStatics(d, b)
  16. }
  17. return function (d, b) {
  18. extendStatics(d, b)
  19. function __() {
  20. this.constructor = d
  21. }
  22. d.prototype =
  23. b === null ? Object.create(b) : ((__.prototype = b.prototype), new __())
  24. }
  25. })()
  26. Object.defineProperty(exports, '__esModule', { value: true })
  27. /** Only accept codes that are numbers, otherwise discard them */
  28. function parseCode(code) {
  29. var number = Number(code)
  30. if (isNaN(number)) return null
  31. return number
  32. }
  33. /** Fetch the code from the value */
  34. function fetchCode(value) {
  35. return (
  36. value &&
  37. (parseCode(value.exitCode) ||
  38. parseCode(value.errno) ||
  39. parseCode(value.code))
  40. )
  41. }
  42. /** Prevent [a weird error on node version 4](https://github.com/bevry/errlop/issues/1) and below. */
  43. function isValid(value) {
  44. /* eslint no-use-before-define:0 */
  45. return value instanceof Error || Errlop.isErrlop(value)
  46. }
  47. var Errlop = /** @class */ (function (_super) {
  48. __extends(Errlop, _super)
  49. /**
  50. * Create an instance of an error, using a message, as well as an optional parent.
  51. * If the parent is provided, then the `fullStack` property will include its stack too
  52. */
  53. function Errlop(input, parent) {
  54. var _this = this
  55. if (!input) throw new Error('Attempted to create an Errlop without a input')
  56. // Instantiate with the above
  57. _this = _super.call(this, input.message || input) || this
  58. // Apply
  59. _this.klass = Errlop
  60. _this.parent = parent || input.parent
  61. _this.ancestors = []
  62. var ancestor = _this.parent
  63. while (ancestor) {
  64. _this.ancestors.push(ancestor)
  65. ancestor = ancestor.parent
  66. }
  67. // this code must support node 0.8, as well as prevent a weird bug in node v4
  68. // https://travis-ci.org/bevry/editions/jobs/408828147
  69. var exitCode = fetchCode(input)
  70. if (exitCode == null) exitCode = fetchCode(_this)
  71. for (
  72. var index = 0;
  73. index < _this.ancestors.length && exitCode == null;
  74. ++index
  75. ) {
  76. var error = _this.ancestors[index]
  77. if (isValid(error)) exitCode = fetchCode(error)
  78. }
  79. // Apply
  80. if (exitCode != null) {
  81. _this.exitCode = exitCode
  82. }
  83. _this.orphanStack = (input.stack || _this.stack).toString()
  84. _this.stack = _this.ancestors.reduce(function (accumulator, error) {
  85. return (
  86. accumulator + '\n\u21B3 ' + (error.orphanStack || error.stack || error)
  87. )
  88. }, _this.orphanStack)
  89. return _this
  90. }
  91. /**
  92. * Syntatic sugar for Errlop class creation.
  93. * Enables `Errlop.create(...args)` to achieve `new Errlop(...args)`
  94. */
  95. Errlop.create = function (input, parent) {
  96. return new this(input, parent)
  97. }
  98. /** Check whether or not the value is an Errlop instance */
  99. Errlop.isErrlop = function (value) {
  100. return value && (value instanceof this || value.klass === this)
  101. }
  102. /** Ensure that the value is an Errlop instance */
  103. Errlop.ensure = function (value) {
  104. return this.isErrlop(value) ? value : this.create(value)
  105. }
  106. return Errlop
  107. })(Error)
  108. exports.default = Errlop