{"record":{"id":"3232fe1161e2066a","repo":"BabylonJS/Babylon.js","slug":"context-failed-to-find-index-index","errorCode":null,"errorMessage":"${context}: Failed to find index (${index})","messagePattern":"(.+?): Failed to find index \\((.+?)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/loaders/src/glTF/2.0/glTFLoader.pure.ts","lineNumber":115,"sourceCode":"interface IWithMetadata {\r\n    metadata: any;\r\n    _internalMetadata: any;\r\n}\r\n\r\n/**\r\n * Helper class for working with arrays when loading the glTF asset\r\n */\r\nexport class ArrayItem {\r\n    /**\r\n     * Gets an item from the given array.\r\n     * @param context The context when loading the asset\r\n     * @param array The array to get the item from\r\n     * @param index The index to the array\r\n     * @returns The array item\r\n     */\r\n    public static Get<T>(context: string, array: ArrayLike<T> | undefined, index: number | undefined): T {\r\n        if (!array || index == undefined || !array[index]) {\r\n            throw new Error(`${context}: Failed to find index (${index})`);\r\n        }\r\n\r\n        return array[index];\r\n    }\r\n\r\n    /**\r\n     * Gets an item from the given array or returns null if not available.\r\n     * @param array The array to get the item from\r\n     * @param index The index to the array\r\n     * @returns The array item or null\r\n     */\r\n    public static TryGet<T>(array: ArrayLike<T> | undefined, index: number | undefined): Nullable<T> {\r\n        if (!array || index == undefined || !array[index]) {\r\n            return null;\r\n        }\r\n\r\n        return array[index];\r\n    }\r","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/loaders/src/glTF/2.0/glTFLoader.pure.ts#L97-L133","documentation":"glTFLoader's static Get<T>() helper indexes into a loaded glTF array (buffers, images, nodes, etc.) and throws when the array is missing, the index is undefined, or no item exists at that index. This guards the loader against malformed assets referencing out-of-range or absent resources.","triggerScenarios":"A glTF JSON references an index that doesn't exist — e.g. a mesh primitive accessor index beyond `accessors.length`, `bufferView.buffer` pointing past `buffers`, or `scene.nodes` containing an index not in `nodes`; also calling Get with undefined index/array while data is still loading.","commonSituations":"Corrupt or hand-edited glTF files; assets produced by buggy exporters leaving dangling indices; partial loads where binary data hasn't arrived but JSON indices are resolved; using loader state before the JSON is parsed.","solutions":["Validate the glTF asset with a validator (e.g. glTF-Validator) to find dangling indices","Re-export the asset from the original DCC tool / exporter","Log `context` from the message — it names which array/index failed, then inspect that part of the JSON","Ensure loading completed (await the load promise) before code that resolves indices runs"],"exampleFix":"// before\nconst buffer = buffers[gltf.bufferViews[i].buffer]; // may be undefined\n// after\nconst idx = gltf.bufferViews[i].buffer;\nif (!buffers || buffers[idx] === undefined) throw new Error(`bufferView ${i} references missing buffer ${idx}`);\nconst buffer = buffers[idx];","handlingStrategy":"type-guard","validationCode":"function validateIndex<T>(array: ArrayLike<T> | undefined, index: number | undefined, context: string): void {\n    if (!array || index == null || !(index in array) || array[index] == null) {\n        throw new Error(`${context}: missing item at index ${index} — validate the asset with glTF-Validator before loading`);\n    }\n}","typeGuard":"function hasIndex<T>(array: ArrayLike<T> | undefined, index: number | undefined): array is ArrayLike<T> {\n    return !!array && index != null && index >= 0 && index < array.length && array[index] != null;\n}","tryCatchPattern":"try {\n    await gltfLoader.loadAsync(container, url);\n} catch (e) {\n    if (String(e.message).includes(\"Failed to find index\")) {\n        console.error(`Malformed glTF asset: ${e.message} — re-export or validate with glTF-Validator`);\n    } else throw e;\n}","preventionTips":["Always run glTF-Validator on assets before shipping them","Re-export assets that produce dangling index references from exporters","Never hand-edit glTF JSON indices without revalidating","Await the load promise before any code that reads loader-resolved resources"],"tags":["gltf","loader","index-out-of-range","asset-validation"],"backgroundTag":"gltf-index-not-found","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}