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

  1. Read error.line and error.source to locate and fix the offending input line
  2. Inspect error.cause for the underlying raw error
  3. 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

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


AI-assisted analysis of toon-format/toon@604eac266e (2026-08-31). Data as JSON: /api/errors/abc73fc9e3c2f1a3. Report an issue: GitHub.