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.

index.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var util = require('util');
  2. var colors = require('ansi-colors');
  3. var extend = require('extend-shallow');
  4. var differ = require('arr-diff');
  5. var union = require('arr-union');
  6. var nonEnum = ['message', 'name', 'stack'];
  7. var ignored = union(nonEnum, ['__safety', '_stack', 'plugin', 'showProperties', 'showStack']);
  8. var props = ['fileName', 'lineNumber', 'message', 'name', 'plugin', 'showProperties', 'showStack', 'stack'];
  9. function PluginError(plugin, message, options) {
  10. if (!(this instanceof PluginError)) {
  11. throw new Error('Call PluginError using new');
  12. }
  13. Error.call(this);
  14. var opts = setDefaults(plugin, message, options);
  15. var self = this;
  16. // If opts has an error, get details from it
  17. if (typeof opts.error === 'object') {
  18. var keys = union(Object.keys(opts.error), nonEnum);
  19. // These properties are not enumerable, so we have to add them explicitly.
  20. keys.forEach(function(prop) {
  21. self[prop] = opts.error[prop];
  22. });
  23. }
  24. // Opts object can override
  25. props.forEach(function(prop) {
  26. if (prop in opts) {
  27. this[prop] = opts[prop];
  28. }
  29. }, this);
  30. // Defaults
  31. if (!this.name) {
  32. this.name = 'Error';
  33. }
  34. if (!this.stack) {
  35. /**
  36. * `Error.captureStackTrace` appends a stack property which
  37. * relies on the toString method of the object it is applied to.
  38. *
  39. * Since we are using our own toString method which controls when
  40. * to display the stack trace, if we don't go through this safety
  41. * object we'll get stack overflow problems.
  42. */
  43. var safety = {};
  44. safety.toString = function() {
  45. return this._messageWithDetails() + '\nStack:';
  46. }.bind(this);
  47. Error.captureStackTrace(safety, arguments.callee || this.constructor);
  48. this.__safety = safety;
  49. }
  50. if (!this.plugin) {
  51. throw new Error('Missing plugin name');
  52. }
  53. if (!this.message) {
  54. throw new Error('Missing error message');
  55. }
  56. }
  57. util.inherits(PluginError, Error);
  58. /**
  59. * Output a formatted message with details
  60. */
  61. PluginError.prototype._messageWithDetails = function() {
  62. var msg = 'Message:\n ' + this.message;
  63. var details = this._messageDetails();
  64. if (details !== '') {
  65. msg += '\n' + details;
  66. }
  67. return msg;
  68. };
  69. /**
  70. * Output actual message details
  71. */
  72. PluginError.prototype._messageDetails = function() {
  73. if (!this.showProperties) {
  74. return '';
  75. }
  76. var props = differ(Object.keys(this), ignored);
  77. var len = props.length;
  78. if (len === 0) {
  79. return '';
  80. }
  81. var res = '';
  82. var i = 0;
  83. while (len--) {
  84. var prop = props[i++];
  85. res += ' ';
  86. res += prop + ': ' + this[prop];
  87. res += '\n';
  88. }
  89. return 'Details:\n' + res;
  90. };
  91. /**
  92. * Override the `toString` method
  93. */
  94. PluginError.prototype.toString = function() {
  95. var detailsWithStack = function(stack) {
  96. return this._messageWithDetails() + '\nStack:\n' + stack;
  97. }.bind(this);
  98. var msg = '';
  99. if (this.showStack) {
  100. // If there is no wrapped error, use the stack captured in the PluginError ctor
  101. if (this.__safety) {
  102. msg = this.__safety.stack;
  103. } else if (this._stack) {
  104. msg = detailsWithStack(this._stack);
  105. } else {
  106. // Stack from wrapped error
  107. msg = detailsWithStack(this.stack);
  108. }
  109. return message(msg, this);
  110. }
  111. msg = this._messageWithDetails();
  112. return message(msg, this);
  113. };
  114. // Format the output message
  115. function message(msg, thisArg) {
  116. var sig = colors.red(thisArg.name);
  117. sig += ' in plugin ';
  118. sig += '"' + colors.cyan(thisArg.plugin) + '"';
  119. sig += '\n';
  120. sig += msg;
  121. return sig;
  122. }
  123. /**
  124. * Set default options based on arguments.
  125. */
  126. function setDefaults(plugin, message, opts) {
  127. if (typeof plugin === 'object') {
  128. return defaults(plugin);
  129. }
  130. opts = opts || {};
  131. if (message instanceof Error) {
  132. opts.error = message;
  133. } else if (typeof message === 'object') {
  134. opts = message;
  135. } else {
  136. opts.message = message;
  137. }
  138. opts.plugin = plugin;
  139. return defaults(opts);
  140. }
  141. /**
  142. * Extend default options with:
  143. *
  144. * - `showStack`: default=false
  145. * - `showProperties`: default=true
  146. *
  147. * @param {Object} `opts` Options to extend
  148. * @return {Object}
  149. */
  150. function defaults(opts) {
  151. return extend({
  152. showStack: false,
  153. showProperties: true,
  154. }, opts);
  155. }
  156. /**
  157. * Expose `PluginError`
  158. */
  159. module.exports = PluginError;