Open History to see the last commit
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 }
15}
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 }
26}
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 }
35}
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 }
44}
45
46export type PersistedCollectionDurabilityErrorOptions = {
47 cause?: unknown
48 code?: unknown
49 path?: unknown
50}
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 }
67}
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 )
90}
91
92export type IndeterminateCommitRequestType =
93 | `rpc:applyLocalMutations:req`
94 | `rpc:applyCommittedTx:req`
95
96export type IndeterminateCommitErrorOptions = {
97 collectionId: string
98 requestType: IndeterminateCommitRequestType
99 previousLeaderId: string | null
100 previousTerm: number | null
101 currentLeaderId: string | null
102 currentTerm: number | null
103 cause: unknown
104}
105
106export class IndeterminateCommitError extends PersistedCollectionCoreError {
107 readonly code: `INDETERMINATE_COMMIT` = `INDETERMINATE_COMMIT`
108 readonly collectionId: string
109 readonly requestType: IndeterminateCommitRequestType
110 readonly previousLeaderId: string | null
111 readonly previousTerm: number | null
112 readonly currentLeaderId: string | null
113 readonly currentTerm: number | null
114 override readonly cause: unknown
115
116 constructor(options: IndeterminateCommitErrorOptions) {
117 super(
118 `Commit outcome is indeterminate for collection "${options.collectionId}": ${options.requestType} crossed leadership from ${formatLeader(options.previousLeaderId, options.previousTerm)} to ${formatLeader(options.currentLeaderId, options.currentTerm)}`,
119 )
120 this.name = `IndeterminateCommitError`
121 this.collectionId = options.collectionId
122 this.requestType = options.requestType
123 this.previousLeaderId = options.previousLeaderId
124 this.previousTerm = options.previousTerm
125 this.currentLeaderId = options.currentLeaderId
126 this.currentTerm = options.currentTerm
127 this.cause = options.cause
128 }
129}
130
131function formatLeader(leaderId: string | null, term: number | null): string {
132 return `${leaderId ?? `unknown leader`} (term ${term ?? `unknown`})`
133}
134
135export class DuplicateRemoteSubsetOwnerError extends PersistedCollectionCoreError {
136 readonly collectionId: string
137
138 constructor(collectionId: string) {
139 super(
140 `A remote subset owner is already registered for collection "${collectionId}"`,
141 )
142 this.name = `DuplicateRemoteSubsetOwnerError`
143 this.collectionId = collectionId
144 }
145}
146
147export class RetryableRemoteSubsetAcquisitionError extends PersistedCollectionCoreError {
148 override readonly cause: unknown
149
150 constructor(message: string, cause?: unknown) {
151 super(message)
152 this.name = `RetryableRemoteSubsetAcquisitionError`
153 this.cause = cause
154 }
155}
156
157export class InvalidPersistedStorageKeyError extends InvalidPersistedCollectionConfigError {
158 constructor(key: string | number) {
159 super(
160 `Invalid persisted storage key "${String(key)}": numeric keys must be finite`,
161 )
162 this.name = `InvalidPersistedStorageKeyError`
163 }
164}
165
166export class InvalidPersistedStorageKeyEncodingError extends InvalidPersistedCollectionConfigError {
167 constructor(encoded: string) {
168 super(
169 `Invalid persisted storage key encoding "${encoded}": expected prefix "n:" or "s:"`,
170 )
171 this.name = `InvalidPersistedStorageKeyEncodingError`
172 }
173}
174
175export class SQLiteBigIntOutOfRangeError extends PersistedCollectionCoreError {
176 constructor(value: bigint, minimum: bigint, maximum: bigint) {
177 super(
178 `SQLite BigInt value ${value} is outside the signed 64-bit range [${minimum}, ${maximum}]`,
179 )
180 this.name = `SQLiteBigIntOutOfRangeError`
181 }
182}
183
184export class PersistenceUnavailableError extends PersistedCollectionCoreError {
185 constructor(details?: string) {
186 super(
187 details
188 ? `Persistence unavailable: ${details}`
189 : `Persistence unavailable in this runtime`,
190 )
191 this.name = `PersistenceUnavailableError`
192 }
193}