UNPKG

1.23 kBJavaScriptView Raw
1'use strict'
2
3/**
4 * Create a range error with the message:
5 * 'Index out of range (index < min)'
6 * 'Index out of range (index < max)'
7 *
8 * @param {number} index The actual index
9 * @param {number} [min=0] Minimum index (included)
10 * @param {number} [max] Maximum index (excluded)
11 * @extends RangeError
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 if (arguments.length < 3) {
20 this.min = 0
21 this.max = min
22 } else {
23 this.min = min
24 this.max = max
25 }
26
27 if (this.min !== undefined && this.index < this.min) {
28 this.message = 'Index out of range (' + this.index + ' < ' + this.min + ')'
29 } else if (this.max !== undefined && this.index >= this.max) {
30 this.message = 'Index out of range (' + this.index + ' > ' + (this.max - 1) + ')'
31 } else {
32 this.message = 'Index out of range (' + this.index + ')'
33 }
34
35 this.stack = (new Error()).stack
36}
37
38IndexError.prototype = new RangeError()
39IndexError.prototype.constructor = RangeError
40IndexError.prototype.name = 'IndexError'
41IndexError.prototype.isIndexError = true
42
43module.exports = IndexError