Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / yargs / node_modules / read-pkg-up / node_modules / read-pkg / node_modules / load-json-file / node_modules / parse-json / node_modules / error-ex / index.js
root on 7 May 2019 2 KB Initial commit
  1. 'use strict';
  2.  
  3. var util = require('util');
  4. var isArrayish = require('is-arrayish');
  5.  
  6. var errorEx = function errorEx(name, properties) {
  7. if (!name || name.constructor !== String) {
  8. properties = name || {};
  9. name = Error.name;
  10. }
  11.  
  12. var errorExError = function ErrorEXError(message) {
  13. if (!this) {
  14. return new ErrorEXError(message);
  15. }
  16.  
  17. message = message instanceof Error
  18. ? message.message
  19. : (message || this.message);
  20.  
  21. Error.call(this, message);
  22. Error.captureStackTrace(this, errorExError);
  23.  
  24. this.name = name;
  25.  
  26. Object.defineProperty(this, 'message', {
  27. configurable: true,
  28. enumerable: false,
  29. get: function () {
  30. var newMessage = message.split(/\r?\n/g);
  31.  
  32. for (var key in properties) {
  33. if (!properties.hasOwnProperty(key)) {
  34. continue;
  35. }
  36.  
  37. var modifier = properties[key];
  38.  
  39. if ('message' in modifier) {
  40. newMessage = modifier.message(this[key], newMessage) || newMessage;
  41. if (!isArrayish(newMessage)) {
  42. newMessage = [newMessage];
  43. }
  44. }
  45. }
  46.  
  47. return newMessage.join('\n');
  48. },
  49. set: function (v) {
  50. message = v;
  51. }
  52. });
  53.  
  54. var stackDescriptor = Object.getOwnPropertyDescriptor(this, 'stack');
  55. var stackGetter = stackDescriptor.get;
  56. var stackValue = stackDescriptor.value;
  57. delete stackDescriptor.value;
  58. delete stackDescriptor.writable;
  59.  
  60. stackDescriptor.get = function () {
  61. var stack = (stackGetter)
  62. ? stackGetter.call(this).split(/\r?\n+/g)
  63. : stackValue.split(/\r?\n+/g);
  64.  
  65. // starting in Node 7, the stack builder caches the message.
  66. // just replace it.
  67. stack[0] = this.name + ': ' + this.message;
  68.  
  69. var lineCount = 1;
  70. for (var key in properties) {
  71. if (!properties.hasOwnProperty(key)) {
  72. continue;
  73. }
  74.  
  75. var modifier = properties[key];
  76.  
  77. if ('line' in modifier) {
  78. var line = modifier.line(this[key]);
  79. if (line) {
  80. stack.splice(lineCount++, 0, ' ' + line);
  81. }
  82. }
  83.  
  84. if ('stack' in modifier) {
  85. modifier.stack(this[key], stack);
  86. }
  87. }
  88.  
  89. return stack.join('\n');
  90. };
  91.  
  92. Object.defineProperty(this, 'stack', stackDescriptor);
  93. };
  94.  
  95. if (Object.setPrototypeOf) {
  96. Object.setPrototypeOf(errorExError.prototype, Error.prototype);
  97. Object.setPrototypeOf(errorExError, Error);
  98. } else {
  99. util.inherits(errorExError, Error);
  100. }
  101.  
  102. return errorExError;
  103. };
  104.  
  105. errorEx.append = function (str, def) {
  106. return {
  107. message: function (v, message) {
  108. v = v || def;
  109.  
  110. if (v) {
  111. message[0] += ' ' + str.replace('%s', v.toString());
  112. }
  113.  
  114. return message;
  115. }
  116. };
  117. };
  118.  
  119. errorEx.line = function (str, def) {
  120. return {
  121. line: function (v) {
  122. v = v || def;
  123.  
  124. if (v) {
  125. return str.replace('%s', v.toString());
  126. }
  127.  
  128. return null;
  129. }
  130. };
  131. };
  132.  
  133. module.exports = errorEx;
Buy Me A Coffee