travels3.0
Guides

Error Handling

Handle typed operation errors, persistence failures, warnings, and observer errors without corrupting history.

Travels separates operation errors, persistence decoding errors, non-fatal warnings, and observer failures. Match stable code values rather than production error message text.

Operation errors

TravelsError extends Error; TravelsTypeError extends TypeError. Both expose a stable code and optional cause.

import { TravelsError, TravelsTypeError } from 'travels';

try {
  travels.transaction(updateDocument);
} catch (error) {
  if (error instanceof TravelsError || error instanceof TravelsTypeError) {
    console.error(error.code, error.cause);
  }
  throw error;
}

Common codes include:

CodeTypical meaning
ASYNC_CALLBACKAn updater or transaction returned asynchronous work.
INVALID_OPTIONFactory or restore options are incompatible or malformed.
INVALID_OPERATIONThe requested operation is not allowed for that capability or state.
UNSUPPORTED_STATEState or a patch path violates the runtime state contract.
PERSISTENCE_INCOMPATIBLEState, patches, paths, or metadata are not durable.
HISTORY_REPLAY_FAILEDA retained patch could not be applied during navigation or reconstruction.
TRANSACTION_FAILEDThe atomic operation failed; inspect cause.
REENTRANT_MUTATIONA synchronous observer or controlled callback attempted a nested mutation.
INVALID_PATCH_ENTRY / EMPTY_PATCH_ENTRYA controlled patch pair is malformed or missing required operations.
UNCLONEABLE_PATCH_VALUEA controlled patch value cannot be detached safely.
CONTROLLED_JOURNAL_REQUIREDAn operation requires a controlled journal.
CONTROLLED_OPERATION_UNAVAILABLEA state-owning capability was requested from a journal.

For asynchronous transaction callbacks, catch TravelsError with code TRANSACTION_FAILED. Its cause is a TravelsTypeError with code ASYNC_CALLBACK for both native async functions and callbacks that return a promise.

Use onError for wrapped core operation failures, especially when coordinating nested transactions. Keep local try/catch around actions whose errors the UI must handle synchronously; do not assume a hook replaces the thrown error.

Persistence errors

TravelsPersistenceError covers decoding, migration, and validation:

CodeMeaning
PARSE_ERRORJSON input could not be parsed.
UNSUPPORTED_VERSIONThe snapshot schema version is not supported.
INVALID_SCHEMASnapshot fields, metadata, or position are malformed.
INVALID_PATCHESPatch containers, operations, paths, or values are invalid.
INVALID_HISTORYSemantic replay or reversibility failed.
MIGRATION_FAILEDThe migration callback failed or returned an invalid/asynchronous result.
FALLBACK_FAILEDRecovery failed or produced an invalid/asynchronous result.

Entry-specific semantic failures can also expose entryIndex and direction. See Migration and Recovery.

Warnings

onWarning receives non-fatal diagnostics with stable codes:

const travels = createTravels(initialState, {
  onWarning({ code, message }) {
    diagnostics.log({ code, message });
  },
});

Warning codes include HISTORY_DISABLED, HISTORY_DISCARDED, HISTORY_TRIMMED, INVALID_POSITION, MUTABLE_FALLBACK, MUTABLE_ROOT_REPLACEMENT, PERSISTENCE_COMPATIBILITY, and POSITION_CLAMPED.

Without the hook, warnings are written to the console only in development. Disabling compatibility warnings does not disable runtime correctness checks.

Observer failures

Errors thrown by listeners, devtools, branch-discard hooks, error/warning hooks, or compatibility checks after commit are routed to onObserverError when configured. They do not interrupt other observers or roll back an already committed transition. Without the hook, development builds log the observer source and the original error to console.error.

const travels = createTravels(initialState, {
  onObserverError({ source, error }) {
    reportError({ source, error });
  },
});

Observer Promises are not awaited, but rejected Promises are isolated and reported. The application remains responsible for actual asynchronous side-effect recovery.

Recovery by boundary

  • Invalid caller input: fix options or state before constructing the timeline; do not continue with a partially initialized owner.
  • Failed transaction: Travels rolls back writes made through its APIs. Application side effects and direct mutable-store writes need separate handling.
  • Unverified persisted history: use validation: 'semantic' and a known-safe fallback before restore.
  • Controlled ingress: call assertTravelHistoryEntryRecordable() before an external commit when the owner cannot roll back a published change.
  • Failed controlled apply: Travels does not advance its own cursor, but cannot undo side effects already performed by the external owner.
  • Replay failure in mutable mode: writes made before the failing patch can remain. Prefer semantic validation before restoring unverified snapshots.

For integrity and tamper resistance, verify encoded bytes outside Travels before deserialization; replay consistency alone does not prove provenance.

On this page