UNPKG

1.2 kBJavaScriptView Raw
1'use strict'
2
3/**
4 * Create a range error with the message:
5 * 'Dimension mismatch (<actual size> != <expected size>)'
6 * @param {number | number[]} actual The actual size
7 * @param {number | number[]} expected The expected size
8 * @param {string} [relation='!='] Optional relation between actual
9 * and expected size: '!=', '<', etc.
10 * @extends RangeError
11 */
12function DimensionError (actual, expected, relation) {
13 if (!(this instanceof DimensionError)) {
14 throw new SyntaxError('Constructor must be called with the new operator')
15 }
16
17 this.actual = actual
18 this.expected = expected
19 this.relation = relation
20
21 this.message = 'Dimension mismatch (' +
22 (Array.isArray(actual) ? ('[' + actual.join(', ') + ']') : actual) +
23 ' ' + (this.relation || '!=') + ' ' +
24 (Array.isArray(expected) ? ('[' + expected.join(', ') + ']') : expected) +
25 ')'
26
27 this.stack = (new Error()).stack
28}
29
30DimensionError.prototype = new RangeError()
31DimensionError.prototype.constructor = RangeError
32DimensionError.prototype.name = 'DimensionError'
33DimensionError.prototype.isDimensionError = true
34
35module.exports = DimensionError