UNPKG

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