PluginError.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var util = require('util');
  2. var arrayDiffer = require('array-differ');
  3. var arrayUniq = require('array-uniq');
  4. var chalk = require('chalk');
  5. var objectAssign = require('object-assign');
  6. var nonEnumberableProperties = ['name', 'message', 'stack'];
  7. var propertiesNotToDisplay = nonEnumberableProperties.concat(['plugin', 'showStack', 'showProperties', '__safety', '_stack']);
  8. // wow what a clusterfuck
  9. var parseOptions = function(plugin, message, opt) {
  10. opt = opt || {};
  11. if (typeof plugin === 'object') {
  12. opt = plugin;
  13. } else {
  14. if (message instanceof Error) {
  15. opt.error = message;
  16. } else if (typeof message === 'object') {
  17. opt = message;
  18. } else {
  19. opt.message = message;
  20. }
  21. opt.plugin = plugin;
  22. }
  23. return objectAssign({
  24. showStack: false,
  25. showProperties: true
  26. }, opt);
  27. };
  28. function PluginError(plugin, message, opt) {
  29. if (!(this instanceof PluginError)) throw new Error('Call PluginError using new');
  30. Error.call(this);
  31. var options = parseOptions(plugin, message, opt);
  32. var self = this;
  33. // if options has an error, grab details from it
  34. if (options.error) {
  35. // These properties are not enumerable, so we have to add them explicitly.
  36. arrayUniq(Object.keys(options.error).concat(nonEnumberableProperties))
  37. .forEach(function(prop) {
  38. self[prop] = options.error[prop];
  39. });
  40. }
  41. var properties = ['name', 'message', 'fileName', 'lineNumber', 'stack', 'showStack', 'showProperties', 'plugin'];
  42. // options object can override
  43. properties.forEach(function(prop) {
  44. if (prop in options) this[prop] = options[prop];
  45. }, this);
  46. // defaults
  47. if (!this.name) this.name = 'Error';
  48. if (!this.stack) {
  49. // Error.captureStackTrace appends a stack property which relies on the toString method of the object it is applied to.
  50. // Since we are using our own toString method which controls when to display the stack trace if we don't go through this
  51. // safety object, then we'll get stack overflow problems.
  52. var safety = {
  53. toString: function() {
  54. return this._messageWithDetails() + '\nStack:';
  55. }.bind(this)
  56. };
  57. Error.captureStackTrace(safety, arguments.callee || this.constructor);
  58. this.__safety = safety;
  59. }
  60. if (!this.plugin) throw new Error('Missing plugin name');
  61. if (!this.message) throw new Error('Missing error message');
  62. }
  63. util.inherits(PluginError, Error);
  64. PluginError.prototype._messageWithDetails = function() {
  65. var messageWithDetails = 'Message:\n ' + this.message;
  66. var details = this._messageDetails();
  67. if (details !== '') {
  68. messageWithDetails += '\n' + details;
  69. }
  70. return messageWithDetails;
  71. };
  72. PluginError.prototype._messageDetails = function() {
  73. if (!this.showProperties) {
  74. return '';
  75. }
  76. var properties = arrayDiffer(Object.keys(this), propertiesNotToDisplay);
  77. if (properties.length === 0) {
  78. return '';
  79. }
  80. var self = this;
  81. properties = properties.map(function stringifyProperty(prop) {
  82. return ' ' + prop + ': ' + self[prop];
  83. });
  84. return 'Details:\n' + properties.join('\n');
  85. };
  86. PluginError.prototype.toString = function () {
  87. var sig = chalk.red(this.name) + ' in plugin \'' + chalk.cyan(this.plugin) + '\'';
  88. var detailsWithStack = function(stack) {
  89. return this._messageWithDetails() + '\nStack:\n' + stack;
  90. }.bind(this);
  91. var msg;
  92. if (this.showStack) {
  93. if (this.__safety) { // There is no wrapped error, use the stack captured in the PluginError ctor
  94. msg = this.__safety.stack;
  95. } else if (this._stack) {
  96. msg = detailsWithStack(this._stack);
  97. } else { // Stack from wrapped error
  98. msg = detailsWithStack(this.stack);
  99. }
  100. } else {
  101. msg = this._messageWithDetails();
  102. }
  103. return sig + '\n' + msg;
  104. };
  105. module.exports = PluginError;