Skip to content

Errors ​

Errors are for programmer mistakes. Data that is unavailable never rejects: it comes back as a miss.

ClassThrown when
TscacheErrorthe base class of all below; also: a disposed or lost client, a fetcher module that failed to load, an invalid snapshot
ConfigErroran invalid CacheConfig or ClientOptions; a cache opened with another structure under the same id; a worker that already runs another fetcher module (code 'fetcher-conflict', the only ConfigError with a code; a stale worker script)
InvalidRangeErrora range endpoint that is not a finite number within ±(2^53 − 1), or start > end; a setFinalizedUntil timestamp out of that domain
UnknownCacheErrora cache id the engine does not know
PutErrora batch rejected as a whole; see below
ProtocolMismatchErrorthe worker speaks another protocol version: a stale cached script or package skew; carries clientProtocol and workerProtocol
AuthInvalidErrorthrown by your fetcher, not by the library (The fetcher module)

Errors keep their class across the worker boundary: a PutError thrown in the worker is a PutError in the page, with its fields.

PutError ​

FieldHolds
code'misaligned', 'unsorted', 'duplicate', 'field-mismatch', 'length-mismatch' or 'range-mismatch'
offenderIndexthe index of the first offending timestamp or value; -1 for a structural problem
offenderTimestampthe timestamp at that index, when it is a number
offenderFieldfor a rejected field value ('field-mismatch'), the field it belongs to; unset otherwise
expectedwhat was expected, e.g. "t ≡ 0 (mod 60000)", "a number"
ts
try {
  await cache.put({
    timestamps: [t0 + 30_000],
    fields: { temperature: [20], errors: [0] },
  });
} catch (error) {
  if (error instanceof PutError) {
    console.log(error.code, error.offenderIndex, error.expected);
    // "misaligned" 0 "t ≡ 0 (mod 60000)"
  }
}

Examples ​

ts
try {
  await createClient({ workerUrl });
} catch (error) {
  if (error instanceof ProtocolMismatchError) {
    // A stale cached worker script, or two package versions: reload.
    console.log(error.clientProtocol, error.workerProtocol);
  } else if (error instanceof ConfigError) {
    console.log("bad options:", error.message);
  } else {
    throw error; // TscacheError: e.g. the fetcher module failed to load
  }
}
ts
const cache = await client.cache({
  id: "a",
  interval: 60_000,
  fields: { v: "f64" },
});
try {
  await cache.get({ start: 10, end: 0 });
} catch (error) {
  console.log(error instanceof InvalidRangeError); // true: start > end
}