overleaf/overleaf · error · Chunk.VersionNotFoundError

chunk for ${projectId} v ${version} not found

Error message

chunk for ${projectId} v ${version} not found

What it means

In the postgres chunk store, getChunkForVersion throws Chunk.VersionNotFoundError when no row in the chunks table satisfies start_version <= version <= end_version for the given doc_id. The library throws it because the requested version does not exist in the persisted history.

Source

Thrown at services/history-v1/storage/lib/chunk_store/postgres.js:58

 * Get the metadata for the chunk that contains the given version.
 *
 * @param {string} projectId
 * @param {number} version
 * @param {object} [opts]
 * @param {boolean} [opts.preferNewer] - If the version is at the boundary of
 *        two chunks, return the newer chunk.
 */
async function getChunkForVersion(projectId, version, opts = {}) {
  assert.postgresId(projectId, 'bad projectId')

  const record = await knex('chunks')
    .where('doc_id', parseInt(projectId, 10))
    .where('start_version', '<=', version)
    .where('end_version', '>=', version)
    .orderBy('end_version', opts.preferNewer ? 'desc' : 'asc')
    .first()
  if (!record) {
    throw new Chunk.VersionNotFoundError(projectId, version)
  }
  return chunkFromRecord(record)
}

/**
 * Get the metadata for the chunk that contains the version that was current at
 * the given timestamp.
 *
 * @param {string} projectId
 * @param {Date} timestamp
 */
async function getChunkForTimestamp(projectId, timestamp) {
  assert.postgresId(projectId, 'bad projectId')

  // This query will find the latest chunk after the timestamp (query orders
  // in reverse chronological order), OR the latest chunk
  // This accounts for the case where the timestamp is ahead of the chunk's
  // timestamp and therefore will not return any results

View on GitHub (pinned to 28ad3b03b7)

Solutions

  1. Query the valid range: SELECT MIN(start_version), MAX(end_version) FROM chunks WHERE doc_id = <id>; clamp or reject out-of-range versions.
  2. Confirm the projectId is the numeric postgres doc id (parseInt must yield the right value).
  3. If rows are missing, re-sync project history into the chunk store.
  4. Check the requested version against the project's current history version counter.

Example fix

// before
const chunk = await chunkStore.getChunkForVersion(projectId, 5000) // throws if absent
// after
const range = await knex('chunks').where('doc_id', projectId)
  .min('start_version as s').max('end_version as e').first()
if (!range || version < range.s || version > range.e) {
  return res.status(404).json({error: 'version not found'})
}
const chunk = await chunkStore.getChunkForVersion(projectId, version)
Defensive patterns

Strategy: validation

Validate before calling

const range = await knex('chunks')
  .where('doc_id', parseInt(projectId, 10))
  .min('start_version as minV').max('end_version as maxV').first()
if (!range || version < Number(range.minV) || version > Number(range.maxV)) {
  throw new NotFound(`version ${version} outside stored range for ${projectId}`)
}

Type guard

function isVersionStored(range, version) {
  return range != null && range.minV != null &&
    version >= range.minV && version <= range.maxV
}

Try / catch

try {
  const chunk = await chunkStore.getChunkForVersion(projectId, version)
} catch (err) {
  if (err instanceof Chunk.VersionNotFoundError) {
    return res.status(404).json({error: 'version not found', projectId, version})
  }
  throw err
}

Prevention

When it happens

Trigger: Calling getChunkForVersion(projectId, version) with a version greater than the latest end_version, a version before the first chunk, or a projectId (doc_id) with no chunk rows.

Common situations: Version number from a different project; requesting history before a project was flushed to postgres; truncated history after resync; passing a mongo id where a numeric postgres id is required.

Related errors


AI-assisted analysis of overleaf/overleaf@28ad3b03b7 (2026-09-03). Data as JSON: /api/errors/2a407a196258cc77. Report an issue: GitHub.