{"record":{"id":"fb6fe6cc0495108e","repo":"remotion-dev/remotion","slug":"from-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"from must be a non-negative integer","messagePattern":"from must be a non-negative integer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/studio-server/src/preview-server/routes/insert-element.ts","lineNumber":73,"sourceCode":"\t\telement,\n\t\tinstallationName,\n\t\texpectedFileState,\n\t\tfrom,\n\t\tposition,\n\t\toverwriteExisting,\n\t},\n\tentryPoint,\n\tremotionRoot,\n\tlogLevel,\n}) =>\n\twithSourceFileWriteQueue(async () => {\n\t\ttry {\n\t\t\tvalidateElementInstallPosition(position);\n\t\t\tif (\n\t\t\t\tfrom !== null &&\n\t\t\t\t(!Number.isInteger(from) || !Number.isFinite(from) || from < 0)\n\t\t\t) {\n\t\t\t\tthrow new Error('from must be a non-negative integer');\n\t\t\t}\n\n\t\t\tconst installationMode =\n\t\t\t\telement.installationMode === null\n\t\t\t\t\t? 'wrapped'\n\t\t\t\t\t: element.installationMode;\n\t\t\tconst componentOwnsSequence =\n\t\t\t\tinstallationMode === 'component-owned-sequence';\n\n\t\t\tRenderInternals.Log.trace(\n\t\t\t\t{indent: false, logLevel},\n\t\t\t\t`[insert-element] Received request for compositionFile=\"${compositionFile}\" compositionId=\"${compositionId}\" element=\"${element.slug}\"`,\n\t\t\t);\n\n\t\t\tconst plan = await getElementInstallPlan({\n\t\t\t\tinstallationName,\n\t\t\t\tdestination: {\n\t\t\t\t\ttype: 'current-composition',","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/remotion-dev/remotion/blob/b2f4e34732f3c6c222ea029413e22dc402218cd7/packages/studio-server/src/preview-server/routes/insert-element.ts#L55-L91","documentation":"insertElementHandler inserts an element into a composition at an optional index `from`. The handler validates that `from`, when not null, is a finite non-negative integer. Anything else (negative numbers, floats, NaN, Infinity, non-numbers that slipped past typing) is rejected with this error before the install plan is computed.","triggerScenarios":"Sending an InsertElementRequest to the studio preview server with from = -1, a fractional value like 1.5, NaN/Infinity, or a malformed value from a client that bypasses typing.","commonSituations":"Client code computing the insert index as indexOf() returning -1 and passing it through unguarded; stale client state sending placeholder values; hand-rolled API calls to the studio server.","solutions":["Ensure the caller clamps the index: use Math.max(0, Math.round(index)) and pass null when no position is intended","Fix the client that passes -1 (e.g. from indexOf of a missing element) — convert -1 to null or 0","If calling the HTTP API directly, send a valid integer >= 0 or JSON null"],"exampleFix":"// before\nawait insertElement({from: children.indexOf(el)}); // -1 when missing\n// after\nconst idx = children.indexOf(el);\nawait insertElement({from: idx === -1 ? null : idx});","handlingStrategy":"validation","validationCode":"const validFrom = (from: number | null): number | null =>\n  from === null || (Number.isInteger(from) && Number.isFinite(from) && from >= 0) ? from : null;","typeGuard":"const isValidFrom = (from: unknown): from is number => typeof from === 'number' && Number.isInteger(from) && Number.isFinite(from) && from >= 0;","tryCatchPattern":"try {\n  await studio.insertElement({..., from});\n} catch (e) {\n  if (String(e).includes('from must be a non-negative integer')) {\n    await studio.insertElement({..., from: null}); // retry without position\n  } else throw e;\n}","preventionTips":["Convert -1 results from indexOf() to null before sending an insert request","Clamp and round computed indices: Math.max(0, Math.round(idx))","Validate request bodies client-side before calling the studio insert-element API"],"tags":["validation","api","studio-server"],"backgroundTag":"invalid-parameter","analyzedSha":"b2f4e34732f3c6c222ea029413e22dc402218cd7","analyzedAt":"2026-09-02T16:25:19.997Z","contentChangedAt":"2026-09-02T16:25:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}