{"record":{"id":"6164d48e63f37945","repo":"phaserjs/phaser","slug":"range-error-values-outside-acceptable-range","errorCode":null,"errorMessage":"Range Error: Values outside acceptable range","messagePattern":"Range Error: Values outside acceptable range","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/utils/array/SafeRange.js","lineNumber":34,"sourceCode":" * @param {array} array - The array to check.\n * @param {number} startIndex - The inclusive start index of the range. Must be zero or greater and less than `endIndex`.\n * @param {number} endIndex - The exclusive end index of the range. Must be greater than `startIndex` and no greater than the array length.\n * @param {boolean} [throwError=false] - If `true`, a `Range Error` is thrown when the range is out of bounds instead of returning `false`.\n *\n * @return {boolean} True if the range is safe, otherwise false.\n */\nvar SafeRange = function (array, startIndex, endIndex, throwError)\n{\n    var len = array.length;\n\n    if (startIndex < 0 ||\n        startIndex >= len ||\n        startIndex >= endIndex ||\n        endIndex > len)\n    {\n        if (throwError)\n        {\n            throw new Error('Range Error: Values outside acceptable range');\n        }\n\n        return false;\n    }\n    else\n    {\n        return true;\n    }\n};\n\nmodule.exports = SafeRange;\n","sourceCodeStart":16,"sourceCodeEnd":46,"githubUrl":"https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/utils/array/SafeRange.js#L16-L46","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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);`."],"exampleFix":"// before\nPhaser.Utils.Array.SafeRange(arr, start, start + pageSize, true);\n\n// after\nvar end = Math.min(start + pageSize, arr.length);\nif (start < 0) start = 0;\nif (start < end) Phaser.Utils.Array.SafeRange(arr, start, end, true);","handlingStrategy":"validation","validationCode":"function safeRangeOrClamp(arr, start, end, throwError) {\n  var len = arr.length;\n  if (start < 0) start = 0;\n  if (end > len) end = len;\n  if (start >= end) return throwError ? false : false;\n  return Phaser.Utils.Array.SafeRange(arr, start, end, throwError);\n}","typeGuard":"function isValidRange(arr, start, end) {\n  return start >= 0 && start < arr.length && start < end && end <= arr.length;\n}","tryCatchPattern":"try {\n  Phaser.Utils.Array.SafeRange(arr, start, end, true);\n} catch (e) {\n  if (e.message === 'Range Error: Values outside acceptable range') {\n    end = Math.min(end, arr.length);\n    start = Math.max(0, Math.min(start, end - 1));\n  } else { throw e; }\n}","preventionTips":["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."],"tags":["array","range","bounds","validation","utils"],"backgroundTag":null,"analyzedSha":"41be1e462bc600064e498cba370bfa8c5c055a22","analyzedAt":"2026-08-13T04:23:39.729Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}