Open History to see the last commit
1import type {
2 ElectronPersistenceMethod,
3 ElectronSerializedError,
4} from './protocol'
5
6type ElectronPersistenceErrorOptions = {
7 code?: string | number
8 cause?: unknown
9}
10
11export class ElectronPersistenceError extends Error {
12 readonly code: string | number | undefined
13
14 constructor(message: string, options?: ElectronPersistenceErrorOptions) {
15 super(message, { cause: options?.cause })
16 this.name = `ElectronPersistenceError`
17 this.code = options?.code
18 }
19}
20
21export class UnknownElectronPersistenceCollectionError extends ElectronPersistenceError {
22 readonly collectionId: string
23
24 constructor(collectionId: string) {
25 super(
26 `Unknown electron persistence collection "${collectionId}". Register the collection adapter in the main process host.`,
27 {
28 code: `UNKNOWN_COLLECTION`,
29 },
30 )
31 this.name = `UnknownElectronPersistenceCollectionError`
32 this.collectionId = collectionId
33 }
34}
35
36export class UnsupportedElectronPersistenceMethodError extends ElectronPersistenceError {
37 readonly method: ElectronPersistenceMethod
38 readonly collectionId: string
39
40 constructor(method: ElectronPersistenceMethod, collectionId: string) {
41 super(
42 `Method "${method}" is not supported by the electron persistence adapter for collection "${collectionId}".`,
43 {
44 code: `UNSUPPORTED_METHOD`,
45 },
46 )
47 this.name = `UnsupportedElectronPersistenceMethodError`
48 this.method = method
49 this.collectionId = collectionId
50 }
51}
52
53export class ElectronPersistenceProtocolError extends ElectronPersistenceError {
54 constructor(message: string, options?: ElectronPersistenceErrorOptions) {
55 super(message, {
56 code: options?.code ?? `INVALID_PROTOCOL`,
57 cause: options?.cause,
58 })
59 this.name = `ElectronPersistenceProtocolError`
60 }
61}
62
63export class ElectronPersistenceTimeoutError extends ElectronPersistenceError {
64 constructor(message: string) {
65 super(message, {
66 code: `TIMEOUT`,
67 })
68 this.name = `ElectronPersistenceTimeoutError`
69 }
70}
71
72export class ElectronPersistenceRpcError extends ElectronPersistenceError {
73 readonly method: ElectronPersistenceMethod
74 readonly collectionId: string
75 readonly requestId: string
76 readonly remoteName: string
77
78 constructor(
79 method: ElectronPersistenceMethod,
80 collectionId: string,
81 requestId: string,
82 serializedError: ElectronSerializedError,
83 ) {
84 super(
85 `${serializedError.name}: ${serializedError.message} (method=${method}, collection=${collectionId}, request=${requestId})`,
86 {
87 code: serializedError.code ?? `REMOTE_ERROR`,
88 },
89 )
90 this.name = `ElectronPersistenceRpcError`
91 this.method = method
92 this.collectionId = collectionId
93 this.requestId = requestId
94 this.remoteName = serializedError.name
95 }
96
97 static fromSerialized(
98 method: ElectronPersistenceMethod,
99 collectionId: string,
100 requestId: string,
101 serializedError: ElectronSerializedError,
102 ): ElectronPersistenceRpcError {
103 return new ElectronPersistenceRpcError(
104 method,
105 collectionId,
106 requestId,
107 serializedError,
108 )
109 }
110}