chenglou/pretext · error · Error

Failed to measure ${currentMeta?.id ?? 'corpus'} @ ${width}

Error message

Failed to measure ${currentMeta?.id ?? 'corpus'} @ ${width}

What it means

Thrown inside runSweep() after calling measureWidth(width, { publish: false, updateStats: false }) for each width in the sweep. If measureWidth returns null (no prepared text available) or returns a report with status === 'error', the sweep cannot continue and this throw fires — preferring report.message when present and falling back to the 'Failed to measure ... @ width' template only when report is null. It halts the entire multi-width sweep, so partial rows already computed are discarded.

Source

Thrown at pages/corpus.ts:956

              reasonGuess: report.firstBreakMismatch.reasonGuess,
              oursContext: report.firstBreakMismatch.oursContext,
              browserContext: report.firstBreakMismatch.browserContext,
            },
          }),
  }
}

function runSweep(widths: number[]): void {
  if (currentMeta === null || currentPrepared === null) {
    return
  }

  const font = buildFont(currentMeta)
  const lineHeight = getLineHeight(currentMeta)
  const rows = widths.map(width => {
    const report = measureWidth(width, { publish: false, updateStats: false })
    if (report === null || report.status === 'error') {
      throw new Error(report?.message ?? `Failed to measure ${currentMeta?.id ?? 'corpus'} @ ${width}`)
    }
    return toSweepRow(report)
  })
  const exactCount = rows.filter(row => Math.round(row.diffPx) === 0).length

  stats.textContent =
    `${currentMeta.title} | Sweep: ${exactCount}/${rows.length} exact | ${rows.length - exactCount} nonzero` +
    ` | ${currentText.length.toLocaleString()} chars`

  setReport(withRequestId({
    status: 'ready',
    environment: getEnvironmentFingerprint(),
    corpusId: currentMeta.id,
    sliceStart: currentSliceStart,
    sliceEnd: currentSliceEnd,
    title: currentMeta.title,
    language: currentMeta.language,
    direction: getDirection(currentMeta),

View on GitHub (pinned to ac49b09b7d)

Solutions

  1. Open the same corpus at a single width (?width=) to reproduce the underlying measureWidth error and read its message — the fallback template only appears when report is null.
  2. Ensure fonts are loaded before triggering the sweep; the loadCorpus flow already awaits document.fonts.ready, so avoid invoking runSweep manually before init completes.
  3. Remove the ?font= / ?lineHeight= overrides to confirm the failure is font-specific.
  4. If automating, increase ACCURACY_CHECK_TIMEOUT_MS and retry — transient measurement races on slow machines can surface here.

Example fix

// before
const report = measureWidth(width, { publish: false, updateStats: false })
if (report === null || report.status === 'error') {
  throw new Error(report?.message ?? `Failed to measure ${currentMeta?.id ?? 'corpus'} @ ${width}`)
}

// after (collect failures instead of aborting the whole sweep)
const report = measureWidth(width, { publish: false, updateStats: false })
if (report === null || report.status === 'error') {
  failedWidths.push({ width, message: report?.message ?? `Failed to measure @ ${width}` })
  return null
}
return toSweepRow(report)
Defensive patterns

Strategy: try-catch

Validate before calling

// Before a sweep, confirm measurement is viable at a single width.
function canSweep(): boolean {
  return currentMeta !== null && currentPrepared !== null && currentText.length > 0
}
if (!canSweep()) {
  // skip the sweep
}

Type guard

function isReadyReport(report: ReturnType<typeof measureWidth>): report is NonNullable<typeof report> & { status: 'ready' } {
  return report !== null && report.status === 'ready'
}

Try / catch

// Wrap runSweep so one bad width does not abort the whole sweep.
const failed: Array<{ width: number, message: string }> = []
const rows = widths.flatMap(width => {
  const report = measureWidth(width, { publish: false, updateStats: false })
  if (report === null || report.status === 'error') {
    failed.push({ width, message: report?.message ?? `Failed @ ${width}` })
    return []
  }
  return [toSweepRow(report)]
})
if (failed.length > 0) console.warn('Sweep failures:', failed)

Prevention

When it happens

Trigger: measureWidth returns an error-status report because the diagnostic canvas/div could not be measured (e.g. zero-size viewport, font not ready, prepared segments missing), or returns null because currentPrepared/currentMeta were cleared between the runSweep guard at line 947 and the map callback. Most often the underlying cause is a measurement-time exception inside measureWidth that got caught and converted to an error report.

Common situations: Running a corpus sweep very early before document.fonts.ready resolves; a font override (?font=) pointing at a family that is not installed so canvas measurement yields garbage; an extremely narrow width where the engine and browser disagree and measureWidth's internal consistency check fails; concurrent reinitialization while a sweep is in flight.

Related errors


AI-assisted analysis of chenglou/pretext@ac49b09b7d (2026-08-12). Data as JSON: /api/errors/e273d0f7f3982092. Report an issue: GitHub.