Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

errors.ts

199 lines6.1 KB

1import { TanStackDBError } from '@tanstack/db'
2​
3export class PersistedCollectionCoreError extends TanStackDBError {
4 constructor(message: string) {
5 super(message)
6 this.name = `PersistedCollectionCoreError`
7 }
8}
9​
10export class InvalidPersistedCollectionConfigError extends PersistedCollectionCoreError {
11 constructor(message: string) {
12 super(message)
13 this.name = `InvalidPersistedCollectionConfigError`
14 }
16​
17export class InvalidSyncConfigError extends InvalidPersistedCollectionConfigError {
18 constructor(details?: string) {
19 super(
20 details
21 ? `Invalid sync config: ${details}`
22 : `Invalid sync config: expected an object with a callable sync function`,
23 )
24 this.name = `InvalidSyncConfigError`
25 }
27​
28export class InvalidPersistedCollectionCoordinatorError extends InvalidPersistedCollectionConfigError {
29 constructor(methodName: string) {
30 super(
31 `Invalid persisted collection coordinator: missing required "${methodName}" method`,
32 )
33 this.name = `InvalidPersistedCollectionCoordinatorError`
34 }
36​
37export class InvalidPersistenceAdapterError extends InvalidPersistedCollectionConfigError {
38 constructor(methodName: string) {
39 super(
40 `Invalid persistence adapter: missing required "${methodName}" method`,
41 )
42 this.name = `InvalidPersistenceAdapterError`
43 }
45​
46export type PersistedCollectionDurabilityErrorOptions = {
47 cause?: unknown
48 code?: unknown
49 path?: unknown
51​
52export class PersistedCollectionDurabilityError extends PersistedCollectionCoreError {
53 override readonly cause: unknown
54 readonly code: unknown
55 readonly path: unknown
56​
57 constructor(
58 message: string,
59 options: PersistedCollectionDurabilityErrorOptions = {},
60 ) {
61 super(message)
62 this.name = `PersistedCollectionDurabilityError`
63 this.cause = options.cause
64 this.code = options.code
65 this.path = options.path
66 }
68​
69export function toPersistedCollectionDurabilityError(
70 collectionId: string,
71 cause: unknown,
72): PersistedCollectionDurabilityError {
73 if (cause instanceof PersistedCollectionDurabilityError) {
74 return cause
75 }
76​
77 const details =
78 typeof cause === `object` && cause !== null
79 ? (cause as Record<string, unknown>)
80 : undefined
81 const message = cause instanceof Error ? cause.message : String(cause)
82 return new PersistedCollectionDurabilityError(
83 `Failed to durably persist collection "${collectionId}": ${message}`,
84 {
85 cause,
86 code: details?.code,
87 path: details?.path,
88 },
89 )
91​
92export type IndeterminateCommitRequestType =
93 `rpc:applyLocalMutations:req` | `rpc:applyCommittedTx:req`
94​
95export type IndeterminateCommitErrorOptions = {
96 collectionId: string
97 requestType: IndeterminateCommitRequestType
98 previousLeaderId: string | null
99 previousTerm: number | null
100 currentLeaderId: string | null
101 currentTerm: number | null
102 cause: unknown
104​
105export class IndeterminateCommitError extends PersistedCollectionCoreError {
106 readonly code: `INDETERMINATE_COMMIT` = `INDETERMINATE_COMMIT`
107 readonly collectionId: string
108 readonly requestType: IndeterminateCommitRequestType
109 readonly previousLeaderId: string | null
110 readonly previousTerm: number | null
111 readonly currentLeaderId: string | null
112 readonly currentTerm: number | null
113 override readonly cause: unknown
114​
115 constructor(options: IndeterminateCommitErrorOptions) {
116 super(
117 `Commit outcome is indeterminate for collection "${options.collectionId}": ${options.requestType} crossed leadership from ${formatLeader(options.previousLeaderId, options.previousTerm)} to ${formatLeader(options.currentLeaderId, options.currentTerm)}`,
118 )
119 this.name = `IndeterminateCommitError`
120 this.collectionId = options.collectionId
121 this.requestType = options.requestType
122 this.previousLeaderId = options.previousLeaderId
123 this.previousTerm = options.previousTerm
124 this.currentLeaderId = options.currentLeaderId
125 this.currentTerm = options.currentTerm
126 this.cause = options.cause
127 }
129​
130function formatLeader(leaderId: string | null, term: number | null): string {
131 return `${leaderId ?? `unknown leader`} (term ${term ?? `unknown`})`
133​
134export class DuplicateRemoteSubsetOwnerError extends PersistedCollectionCoreError {
135 readonly collectionId: string
136​
137 constructor(collectionId: string) {
138 super(
139 `A remote subset owner is already registered for collection "${collectionId}"`,
140 )
141 this.name = `DuplicateRemoteSubsetOwnerError`
142 this.collectionId = collectionId
143 }
145​
146export class RetryableRemoteSubsetAcquisitionError extends PersistedCollectionCoreError {
147 override readonly cause: unknown
148​
149 constructor(message: string, cause?: unknown) {
150 super(message)
151 this.name = `RetryableRemoteSubsetAcquisitionError`
152 this.cause = cause
153 }
155​
156export class InvalidPersistedStorageKeyError extends InvalidPersistedCollectionConfigError {
157 constructor(key: string | number) {
158 super(
159 `Invalid persisted storage key "${String(key)}": numeric keys must be finite`,
160 )
161 this.name = `InvalidPersistedStorageKeyError`
162 }
164​
165export class InvalidPersistedStorageKeyEncodingError extends InvalidPersistedCollectionConfigError {
166 constructor(encoded: string) {
167 super(
168 `Invalid persisted storage key encoding "${encoded}": expected prefix "n:" or "s:"`,
169 )
170 this.name = `InvalidPersistedStorageKeyEncodingError`
171 }
173​
174export class SQLiteBigIntOutOfRangeError extends PersistedCollectionCoreError {
175 constructor(value: bigint, minimum: bigint, maximum: bigint) {
176 super(
177 `SQLite BigInt value ${value} is outside the signed 64-bit range [${minimum}, ${maximum}]`,
178 )
179 this.name = `SQLiteBigIntOutOfRangeError`
180 }
182​
183export class SQLiteConnectionClosedError extends PersistedCollectionCoreError {
184 constructor(message: string) {
185 super(message)
186 this.name = `SQLiteConnectionClosedError`
187 }
189​
190export class PersistenceUnavailableError extends PersistedCollectionCoreError {
191 constructor(details?: string) {
192 super(
193 details
194 ? `Persistence unavailable: ${details}`
195 : `Persistence unavailable in this runtime`,
196 )
197 this.name = `PersistenceUnavailableError`
198 }