codex-team/editor.js · error · Error
Incorrect index
Error message
Incorrect index
What it means
BlockCollection.replace(index, block) swaps a block in the internal array; if blocks[index] is undefined (index past the end or negative) it throws 'Incorrect index'. Reached via internal flows like beautifyShortcut and isNodeEmpty handling.
Source
Thrown at src/components/blocks.ts:232
const nextBlock = this.blocks[index + 1];
if (nextBlock) {
this.insertToDOM(block, 'beforebegin', nextBlock);
} else {
this.insertToDOM(block);
}
}
}
/**
* Replaces block under passed index with passed block
*
* @param index - index of existed block
* @param block - new block
*/
public replace(index: number, block: Block): void {
if (this.blocks[index] === undefined) {
throw Error('Incorrect index');
}
const prevBlock = this.blocks[index];
prevBlock.holder.replaceWith(block.holder);
this.blocks[index] = block;
}
/**
* Inserts several blocks at once
*
* @param blocks - blocks to insert
* @param index - index to insert blocks at
*/
public insertMany(blocks: Block[], index: number ): void {
const fragment = new DocumentFragment();
View on GitHub (pinned to 5f45dabbe5)
Solutions
- Refresh the index immediately before calling replace instead of caching it
- Ensure index < blocks.length before replace
- Serialize structural block operations so removals can't interleave
Example fix
// before
blockCollection.replace(staleIndex, newBlock);
// after
if (blockCollection.blocks[staleIndex] !== undefined) {
blockCollection.replace(staleIndex, newBlock);
} Defensive patterns
Strategy: validation
Validate before calling
if (blockCollection.blocks[index] !== undefined) { blockCollection.replace(index, newBlock); } Type guard
const isValidBlockIndex = (blocks: Block[], i: number): boolean => Number.isInteger(i) && i >= 0 && i < blocks.length;
Try / catch
try { blockCollection.replace(index, block); } catch (e) { if (e instanceof Error && e.message === 'Incorrect index') { /* recompute index and retry */ } throw e; } Prevention
- Recompute indices right before structural ops
- Serialize block mutations to avoid races
When it happens
Trigger: An internal operation calling replace with an index no longer valid because blocks were removed concurrently (undo/redo race, rapid deletion), or an out-of-range index computed from a stale count.
Common situations: Race conditions between keyboard shortcuts (beautify/enter handling) and asynchronous block removal; bugs in custom code reaching into BlockManager.replace with stale indices.
Related errors
- Index should be greater than or equal to 0
- Unable to move Block down since it is already the last
- Unable to move Block up since it is already the first
- Index should be a number
AI-assisted analysis of codex-team/editor.js@5f45dabbe5 (2026-08-27).
Data as JSON: /api/errors/362ebd9688fa9778.
Report an issue: GitHub.