phaserjs/phaser · error · Error
Range Error: Values outside acceptable range
Error message
Range Error: Values outside acceptable range
What it means
Thrown by Phaser.Utils.Array.SafeRange ONLY when its fourth argument throwError is truthy; by default it returns false instead of throwing. A range is unsafe when startIndex < 0, startIndex >= array.length, startIndex >= endIndex, or endIndex > array.length. SafeRange is a pre-check helper used by other array utilities (e.g. range-based remove/slice wrappers) that opt into hard failures via the throwError flag.
Source
Thrown at src/utils/array/SafeRange.js:34
* @param {array} array - The array to check.
* @param {number} startIndex - The inclusive start index of the range. Must be zero or greater and less than `endIndex`.
* @param {number} endIndex - The exclusive end index of the range. Must be greater than `startIndex` and no greater than the array length.
* @param {boolean} [throwError=false] - If `true`, a `Range Error` is thrown when the range is out of bounds instead of returning `false`.
*
* @return {boolean} True if the range is safe, otherwise false.
*/
var SafeRange = function (array, startIndex, endIndex, throwError)
{
var len = array.length;
if (startIndex < 0 ||
startIndex >= len ||
startIndex >= endIndex ||
endIndex > len)
{
if (throwError)
{
throw new Error('Range Error: Values outside acceptable range');
}
return false;
}
else
{
return true;
}
};
module.exports = SafeRange;
View on GitHub (pinned to 41be1e462b)
Solutions
- Normalize before calling: clamp start to [0, len-1], set end = Math.min(end, len), and ensure start < end.
- If you only want a boolean check, omit the throwError argument (defaults to false) and branch on the return value.
- Use Array.prototype.slice for read-only sub-arrays; it tolerates out-of-range indices without throwing.
- Validate page math: `end = Math.min(start + pageSize, arr.length);`.
Example fix
// before Phaser.Utils.Array.SafeRange(arr, start, start + pageSize, true); // after var end = Math.min(start + pageSize, arr.length); if (start < 0) start = 0; if (start < end) Phaser.Utils.Array.SafeRange(arr, start, end, true);
Defensive patterns
Strategy: validation
Validate before calling
function safeRangeOrClamp(arr, start, end, throwError) {
var len = arr.length;
if (start < 0) start = 0;
if (end > len) end = len;
if (start >= end) return throwError ? false : false;
return Phaser.Utils.Array.SafeRange(arr, start, end, throwError);
} Type guard
function isValidRange(arr, start, end) {
return start >= 0 && start < arr.length && start < end && end <= arr.length;
} Try / catch
try {
Phaser.Utils.Array.SafeRange(arr, start, end, true);
} catch (e) {
if (e.message === 'Range Error: Values outside acceptable range') {
end = Math.min(end, arr.length);
start = Math.max(0, Math.min(start, end - 1));
} else { throw e; }
} Prevention
- Omit the throwError argument (or pass false) when you only want a boolean check.
- Clamp start/end to array bounds and ensure start < end before the hard-fail call.
- Derive end from Math.min(start + pageSize, arr.length) in pagination code.
- Use Array.prototype.slice for read-only sub-arrays; it tolerates overrun.
When it happens
Trigger: Calling SafeRange(arr, start, end, true) with an invalid range, OR calling a higher-level Phaser array function that internally passes throwError=true. Off-by-one where endIndex is set to array.length+1, or startIndex equals endIndex (empty range, rejected because start must be < end). Passing start > end by accident.
Common situations: Pagination logic that computes start/end from page/size and overruns on the last page. Slicing a sub-array with endIndex derived from a search that returned -1 (then end becomes large). Reacting to a UI selection range where start/end can be equal or inverted. Calling Phaser.Utils.Array.RemoveBetween or similar that delegates to SafeRange with throwError enabled.
Related errors
- Supplied index out of bounds
- Index out of bounds
- Supplied items must be elements of the same array
- Supplied items must be elements of the same array
- Supplied items must be elements of the same array
AI-assisted analysis of phaserjs/phaser@41be1e462b (2026-08-13).
Data as JSON: /api/errors/6164d48e63f37945.
Report an issue: GitHub.