mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
108 lines
3.2 KiB
JavaScript
108 lines
3.2 KiB
JavaScript
|
|
/**
|
|
* Create a syntax error with the message:
|
|
* 'Wrong number of arguments in function <fn> (<count> provided, <min>-<max> expected)'
|
|
* @param {String} name Function name
|
|
* @param {Number} count Actual argument count
|
|
* @param {Number} min Minimum required argument count
|
|
* @param {Number} [max] Maximum required argument count
|
|
* @extends Error
|
|
*/
|
|
function ArgumentsError(name, count, min, max) {
|
|
if (!(this instanceof ArgumentsError)) {
|
|
throw new SyntaxError('Constructor must be called with the new operator');
|
|
}
|
|
|
|
this.message = 'Wrong number of arguments in function ' + name +
|
|
' (' + count + ' provided, ' +
|
|
min + ((max != undefined) ? ('-' + max) : '') + ' expected)';
|
|
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
|
|
ArgumentsError.prototype = new Error();
|
|
ArgumentsError.prototype.constructor = Error;
|
|
ArgumentsError.prototype.name = 'ArgumentsError';
|
|
|
|
exports.ArgumentsError = ArgumentsError;
|
|
|
|
/**
|
|
* Create a range error with the message:
|
|
* 'Dimension mismatch (<actual size> != <expected size>)'
|
|
* @param {number | number[]} actual The actual size
|
|
* @param {number | number[]} expected The expected size
|
|
* @param {string} [relation='!='] Optional relation between actual
|
|
* and expected size: '!=', '<', etc.
|
|
* @extends RangeError
|
|
*/
|
|
function DimensionError(actual, expected, relation) {
|
|
if (!(this instanceof DimensionError)) {
|
|
throw new SyntaxError('Constructor must be called with the new operator');
|
|
}
|
|
|
|
this.actual = actual;
|
|
this.expected = expected;
|
|
this.relation = relation || '!=';
|
|
|
|
this.message = 'Dimension mismatch (' +
|
|
(Array.isArray(actual) ? ('[' + actual.join(', ') + ']') : actual) +
|
|
' ' + this.relation + ' ' +
|
|
(Array.isArray(expected) ? ('[' + expected.join(', ') + ']') : expected) +
|
|
')';
|
|
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
|
|
DimensionError.prototype = new RangeError();
|
|
DimensionError.prototype.constructor = RangeError;
|
|
DimensionError.prototype.name = 'DimensionError';
|
|
|
|
exports.DimensionError = DimensionError;
|
|
|
|
/**
|
|
* Create a range error with the message:
|
|
* 'Index out of range (index < min)'
|
|
* 'Index out of range (index < max)'
|
|
*
|
|
* @param {number} index The actual index
|
|
* @param {number} [min=0] Minimum index (included)
|
|
* @param {number} [max] Maximum index (excluded)
|
|
* @extends RangeError
|
|
*/
|
|
function IndexError(index, min, max) {
|
|
if (!(this instanceof IndexError)) {
|
|
throw new SyntaxError('Constructor must be called with the new operator');
|
|
}
|
|
|
|
this.index = index;
|
|
if (arguments.length <= 2) {
|
|
this.min = 0;
|
|
this.max = min;
|
|
}
|
|
else {
|
|
this.min = min;
|
|
this.max = max;
|
|
}
|
|
|
|
if (this.index < this.min) {
|
|
this.message = 'Index out of range (' + this.index + ' < ' + this.min + ')';
|
|
}
|
|
else if (this.index >= this.max) {
|
|
this.message = 'Index out of range (' + this.index + ' > ' + (this.max - 1) + ')';
|
|
}
|
|
else {
|
|
this.message = 'Index out of range (' + this.index + ')';
|
|
}
|
|
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
|
|
IndexError.prototype = new RangeError();
|
|
IndexError.prototype.constructor = RangeError;
|
|
IndexError.prototype.name = 'IndexError';
|
|
|
|
exports.IndexError = IndexError;
|
|
|
|
|
|
// TODO: implement a InvalidValueError?
|