toon-format/toon · error · ToonDecodeError
error.message
Error message
error.message
What it means
withLine wraps decoding steps and converts any thrown Error (e.g. SyntaxError from header parsing) into a ToonDecodeError enriched with line number, raw source line, and cause. Existing ToonDecodeError instances are rethrown unchanged; non-Error throwables pass through untouched.
Source
Thrown at packages/toon/src/decode/errors.ts:39
}
}
/**
* Runs `fn` and re-throws any non-`ToonDecodeError` `Error` as a `ToonDecodeError`
* with line context attached and the original error preserved as `cause`.
*
* Pure parser helpers don't know which line they're parsing; this wrapper is how
* the streaming decoder enriches their errors.
*/
export function withLine<T>(line: ParsedLine, fn: () => T): T {
try {
return fn()
}
catch (error) {
if (error instanceof ToonDecodeError)
throw error
if (error instanceof Error) {
throw new ToonDecodeError(error.message, {
line: line.lineNumber,
source: line.raw,
cause: error,
})
}
throw error
}
}
View on GitHub (pinned to 604eac266e)
Solutions
- Read error.line and error.source to locate and fix the offending input line
- Inspect error.cause for the underlying raw error
- Catch ToonDecodeError specifically around decode() calls
Example fix
// before
const value = decode(text)
// after
try {
const value = decode(text)
} catch (e) {
if (e instanceof ToonDecodeError) console.error(`Line ${e.line}: ${e.message}`)
} Defensive patterns
Strategy: try-catch
Validate before calling
// Cheap pre-check: input must be a string
if (typeof input !== 'string') throw new TypeError('TOON input must be a string') Type guard
function isToonDecodeError(e: unknown): e is ToonDecodeError {
return e instanceof ToonDecodeError
} Try / catch
try {
const value = decode(text)
} catch (e) {
if (isToonDecodeError(e)) {
console.error(`TOON decode failed at line ${e.line}: ${e.source}`, { cause: e.cause })
} else throw e
} Prevention
- Catch ToonDecodeError (not generic Error) to access line/source metadata
- Log error.cause for the root reason when reporting bugs
- Try-decode user-supplied TOON at ingestion boundaries
- Show error.line + error.source in user-facing diagnostics
When it happens
Trigger: Any internal decoding failure (SyntaxError, TypeError, etc.) inside withLine-wrapped functions: headerInfo, decodeDocument, arrayHeader, decodeKeyValue, values.
Common situations: Malformed TOON input during decode; building user-facing diagnostics that need line numbers; debugging decode failures.
Related errors
- Top-level document must start with a key-value or array-head
- Indentation depth jump: expected depth ${parentDepth + 1}, b
- Unexpected bare token line outside root primitive position
- Unexpected content after the document root
- Duplicate sibling key "${key}"
AI-assisted analysis of toon-format/toon@604eac266e (2026-08-31).
Data as JSON: /api/errors/abc73fc9e3c2f1a3.
Report an issue: GitHub.