UNPKG

1.14 kBJavaScriptView Raw
1/**
2 * Create a range error with the message:
3 * 'Dimension mismatch (<actual size> != <expected size>)'
4 * @param {number | number[]} actual The actual size
5 * @param {number | number[]} expected The expected size
6 * @param {string} [relation='!='] Optional relation between actual
7 * and expected size: '!=', '<', etc.
8 * @extends RangeError
9 */
10export function DimensionError(actual, expected, relation) {
11 if (!(this instanceof DimensionError)) {
12 throw new SyntaxError('Constructor must be called with the new operator');
13 }
14
15 this.actual = actual;
16 this.expected = expected;
17 this.relation = relation;
18 this.message = 'Dimension mismatch (' + (Array.isArray(actual) ? '[' + actual.join(', ') + ']' : actual) + ' ' + (this.relation || '!=') + ' ' + (Array.isArray(expected) ? '[' + expected.join(', ') + ']' : expected) + ')';
19 this.stack = new Error().stack;
20}
21DimensionError.prototype = new RangeError();
22DimensionError.prototype.constructor = RangeError;
23DimensionError.prototype.name = 'DimensionError';
24DimensionError.prototype.isDimensionError = true;
\No newline at end of file