UNPKG

1.21 kBJavaScriptView Raw
1/**
2 * Create a range error with the message:
3 * 'Index out of range (index < min)'
4 * 'Index out of range (index < max)'
5 *
6 * @param {number} index The actual index
7 * @param {number} [min=0] Minimum index (included)
8 * @param {number} [max] Maximum index (excluded)
9 * @extends RangeError
10 */
11export function IndexError(index, min, max) {
12 if (!(this instanceof IndexError)) {
13 throw new SyntaxError('Constructor must be called with the new operator');
14 }
15
16 this.index = index;
17
18 if (arguments.length < 3) {
19 this.min = 0;
20 this.max = min;
21 } else {
22 this.min = min;
23 this.max = max;
24 }
25
26 if (this.min !== undefined && this.index < this.min) {
27 this.message = 'Index out of range (' + this.index + ' < ' + this.min + ')';
28 } else if (this.max !== undefined && this.index >= this.max) {
29 this.message = 'Index out of range (' + this.index + ' > ' + (this.max - 1) + ')';
30 } else {
31 this.message = 'Index out of range (' + this.index + ')';
32 }
33
34 this.stack = new Error().stack;
35}
36IndexError.prototype = new RangeError();
37IndexError.prototype.constructor = RangeError;
38IndexError.prototype.name = 'IndexError';
39IndexError.prototype.isIndexError = true;
\No newline at end of file