UNPKG

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