Errors
Errors are for programmer mistakes. Data that is unavailable never rejects: it comes back as a miss.
| Class | Thrown when |
|---|---|
TscacheError | the base class of all below; also: a disposed or lost client, a fetcher module that failed to load, an invalid snapshot |
ConfigError | an 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) |
InvalidRangeError | a range endpoint that is not a finite number within ±(2^53 − 1), or start > end; a setFinalizedUntil timestamp out of that domain |
UnknownCacheError | a cache id the engine does not know |
PutError | a batch rejected as a whole; see below |
ProtocolMismatchError | the worker speaks another protocol version: a stale cached script or package skew; carries clientProtocol and workerProtocol |
AuthInvalidError | thrown 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
| Field | Holds |
|---|---|
code | 'misaligned', 'unsorted', 'duplicate', 'field-mismatch', 'length-mismatch' or 'range-mismatch' |
offenderIndex | the index of the first offending timestamp or value; -1 for a structural problem |
offenderTimestamp | the timestamp at that index, when it is a number |
offenderField | for a rejected field value ('field-mismatch'), the field it belongs to; unset otherwise |
expected | what 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
}