errors.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. /*
  3. This file is part of web3.js.
  4. web3.js is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. web3.js is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.Web3ValidatorError = void 0;
  17. const web3_errors_1 = require("web3-errors");
  18. const object_js_1 = require("./validation/object.js");
  19. const errorFormatter = (error) => {
  20. if (error.message && error.instancePath && error.params && !(0, object_js_1.isNullish)(error.params.value)) {
  21. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  22. return `value "${error.params.value}" at "${error.instancePath}" ${error.message}`;
  23. }
  24. if (error.message && error.instancePath) {
  25. return `value at "${error.instancePath}" ${error.message}`;
  26. }
  27. if (error.instancePath) {
  28. return `value at "${error.instancePath}" caused unspecified error`;
  29. }
  30. if (error.message) {
  31. return error.message;
  32. }
  33. return 'unspecified error';
  34. };
  35. class Web3ValidatorError extends web3_errors_1.BaseWeb3Error {
  36. constructor(errors) {
  37. super();
  38. this.code = web3_errors_1.ERR_VALIDATION;
  39. this.errors = errors;
  40. super.message = `Web3 validator found ${errors.length} error[s]:\n${this._compileErrors().join('\n')}`;
  41. }
  42. _compileErrors() {
  43. const errorMsgs = this.errors.map(errorFormatter);
  44. return errorMsgs;
  45. }
  46. }
  47. exports.Web3ValidatorError = Web3ValidatorError;
  48. //# sourceMappingURL=errors.js.map