improveErrorMessage.js 1.3 KB

123456789101112131415161718192021222324252627282930
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.improveErrorMessage = improveErrorMessage;
  6. var _is = require("../../../utils/is.js");
  7. /**
  8. * Improve error messages for statistics functions. Errors are typically
  9. * thrown in an internally used function like larger, causing the error
  10. * not to mention the function (like max) which is actually used by the user.
  11. *
  12. * @param {Error} err
  13. * @param {String} fnName
  14. * @param {*} [value]
  15. * @return {Error}
  16. */
  17. function improveErrorMessage(err, fnName, value) {
  18. // TODO: add information with the index (also needs transform in expression parser)
  19. var details;
  20. if (String(err).indexOf('Unexpected type') !== -1) {
  21. details = arguments.length > 2 ? ' (type: ' + (0, _is.typeOf)(value) + ', value: ' + JSON.stringify(value) + ')' : ' (type: ' + err.data.actual + ')';
  22. return new TypeError('Cannot calculate ' + fnName + ', unexpected type of argument' + details);
  23. }
  24. if (String(err).indexOf('complex numbers') !== -1) {
  25. details = arguments.length > 2 ? ' (type: ' + (0, _is.typeOf)(value) + ', value: ' + JSON.stringify(value) + ')' : '';
  26. return new TypeError('Cannot calculate ' + fnName + ', no ordering relation is defined for complex numbers' + details);
  27. }
  28. return err;
  29. }