improveErrorMessage.js 1.1 KB

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