simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

improveErrorMessage.js (1289B)


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