index.js 4.4 KB

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