ValidationError.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. var _chalk = require('chalk');
  7. var _chalk2 = _interopRequireDefault(_chalk);
  8. var _stripAnsi = require('strip-ansi');
  9. var _stripAnsi2 = _interopRequireDefault(_stripAnsi);
  10. var _textTable = require('text-table');
  11. var _textTable2 = _interopRequireDefault(_textTable);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  14. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  15. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  16. var ValidationError = function (_Error) {
  17. _inherits(ValidationError, _Error);
  18. function ValidationError(options) {
  19. _classCallCheck(this, ValidationError);
  20. // Workaround for https://github.com/istanbuljs/istanbuljs/issues/139
  21. var _this = _possibleConstructorReturn(this, (ValidationError.__proto__ || Object.getPrototypeOf(ValidationError)).call(this));
  22. _this.constructor = ValidationError;
  23. _this.__proto__ = ValidationError.prototype;
  24. // End Workaround
  25. // prettier-ignore
  26. _this.message = _chalk2.default`${options.name}`;
  27. _this.meta = options;
  28. _this.meta.desc = _chalk2.default`{underline Options Validation Error}`;
  29. _this.name = 'ValidationError';
  30. if (options.log) {
  31. _this.message += `: ${_this.meta.desc}`;
  32. } else {
  33. _this.message += `\n\n ${_this.meta.desc}\n\n${_this.format()}\n`;
  34. }
  35. Error.captureStackTrace(_this, _this.constructor);
  36. return _this;
  37. }
  38. _createClass(ValidationError, [{
  39. key: 'format',
  40. value: function format() {
  41. var errors = this.meta.errors;
  42. var rows = [];
  43. var options = {
  44. align: ['', 'l', 'l'],
  45. stringLength(str) {
  46. return (0, _stripAnsi2.default)(str).length;
  47. }
  48. };
  49. var _iteratorNormalCompletion = true;
  50. var _didIteratorError = false;
  51. var _iteratorError = undefined;
  52. try {
  53. for (var _iterator = errors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  54. var err = _step.value;
  55. rows.push(['', _chalk2.default`{dim options}${err.dataPath}`, _chalk2.default`{blue ${err.message}}`]);
  56. }
  57. } catch (err) {
  58. _didIteratorError = true;
  59. _iteratorError = err;
  60. } finally {
  61. try {
  62. if (!_iteratorNormalCompletion && _iterator.return) {
  63. _iterator.return();
  64. }
  65. } finally {
  66. if (_didIteratorError) {
  67. throw _iteratorError;
  68. }
  69. }
  70. }
  71. return (0, _textTable2.default)(rows, options);
  72. }
  73. // eslint-disable-next-line class-methods-use-this
  74. }, {
  75. key: 'toString',
  76. value: function toString() {
  77. return `${this.message}\n\n${this.format()}`;
  78. }
  79. }]);
  80. return ValidationError;
  81. }(Error);
  82. exports.default = ValidationError;