Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Errors and fail-closed rules

Every fallible call returns Result<_, HakoError>. The variants:

VariantMeaningTypical cause
IoOS/filesystem failureDisk full, permissions, missing directory.
LockPoisonedInternal lock poisonedA prior panic while holding it — restart the process.
StorageErrorWAL/segment/blob failureTorn files, zstd decode failure, blob resolve miss.
QueryErrorQuery rejected before runningUnsatisfied filters/ordering, unsupported scan shape.
IndexErrorIndex operation failedBuilding on a missing collection, bad definition.
CorruptFail-closed refusalDenied access, out-of-range traverse depth, compressed payload on a compression-less build.
InvalidInputBad argumentEmpty ids, zero depth/limit, malformed reference.
CancelledCooperative cancellation requestedA CancelToken passed to a *_with_cancel method was cancelled.

Design rules behind them (these never change silently):

  1. Loud over empty. A missing index, untracked anchor, or denied collection errors — it never returns a plausible-looking empty result. An empty result always means “no data”.
  2. Ranges are hard errors. Traverse depth outside 1..=32, zero limits, empty ids: rejected, never clamped.
  3. Capability mismatches fail at the boundary. zstd payload on a no-compression build, encrypted room with a keyless peer, old peer in a new protocol: refused with a named error, at open/handshake time where possible.
  4. Panics don’t cross FFI. The C boundary catches panics per call and reports them through hk_last_error — the process survives a bad call.

Handling pattern

match db.get("users", id)? {
    Some(doc) => render(doc),
    None => render_missing(),   // missing is data, not failure
}

Match on None for absence; match on Err for failure. Logging the full HakoError::Corrupt message on failure is encouraged — the messages name the collection, index, or rule involved.

Common mistakes

  • Treating Ok(None) as an error. It means “no such document”. Retrying won’t create it.
  • Swallowing Corrupt with a default. Fail-closed errors are guardrails; defaulting past them reintroduces the exact silent wrongness they prevent. Fix the cause (create the index, distribute the key, fix the range).