Skip to content

Commit b39a73f

Browse files
juliusmarmingecodex
andcommitted
[codex] structure connection storage failures
Co-authored-by: codex <[email protected]>
1 parent daa8ae5 commit b39a73f

6 files changed

Lines changed: 267 additions & 101 deletions

File tree

‎apps/mobile/src/connection/catalog-store.ts‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import {
33
type ConnectionCatalogDocument as ConnectionCatalogDocumentType,
44
EMPTY_CONNECTION_CATALOG_DOCUMENT,
55
} from "@t3tools/client-runtime/platform";
6-
import { ConnectionTransientError } from "@t3tools/client-runtime/connection";
6+
import {
7+
ConnectionStorageOperationError,
8+
ConnectionTransientError,
9+
} from "@t3tools/client-runtime/connection";
710
import * as Effect from "effect/Effect";
811
import * as Option from "effect/Option";
912
import * as Ref from "effect/Ref";
@@ -21,13 +24,14 @@ const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDoc
2124

2225
const decodeCatalog = Effect.fn("mobile.connectionStorage.decodeCatalog")(function* (raw: string) {
2326
return yield* decodeConnectionCatalogDocument(raw).pipe(
24-
Effect.mapError(
25-
(cause) =>
26-
new ConnectionTransientError({
27-
reason: "remote-unavailable",
28-
detail: "Could not decode the local connection catalog.",
27+
Effect.mapError((cause) =>
28+
ConnectionTransientError.fromStorageFailure(
29+
new ConnectionStorageOperationError({
30+
operation: "decode",
31+
backend: "schema",
2932
cause,
3033
}),
34+
),
3135
),
3236
);
3337
});
@@ -36,13 +40,14 @@ const encodeCatalog = Effect.fn("mobile.connectionStorage.encodeCatalog")(functi
3640
catalog: ConnectionCatalogDocumentType,
3741
) {
3842
return yield* encodeConnectionCatalogDocument(catalog).pipe(
39-
Effect.mapError(
40-
(cause) =>
41-
new ConnectionTransientError({
42-
reason: "remote-unavailable",
43-
detail: "Could not encode the local connection catalog.",
43+
Effect.mapError((cause) =>
44+
ConnectionTransientError.fromStorageFailure(
45+
new ConnectionStorageOperationError({
46+
operation: "encode",
47+
backend: "schema",
4448
cause,
4549
}),
50+
),
4651
),
4752
);
4853
});
@@ -72,13 +77,14 @@ export const makeCatalogStore = Effect.fn("mobile.connectionStorage.makeCatalogS
7277
legacyRaw === null || legacyRaw.trim() === ""
7378
? EMPTY_CONNECTION_CATALOG_DOCUMENT
7479
: yield* migrateLegacyConnectionCatalog(legacyRaw).pipe(
75-
Effect.mapError(
76-
(cause) =>
77-
new ConnectionTransientError({
78-
reason: "remote-unavailable",
79-
detail: "Could not migrate the legacy local connection catalog.",
80+
Effect.mapError((cause) =>
81+
ConnectionTransientError.fromStorageFailure(
82+
new ConnectionStorageOperationError({
83+
operation: "migrate",
84+
backend: "legacy-migration",
8085
cause,
8186
}),
87+
),
8288
),
8389
Effect.catchTags({
8490
ConnectionTransientError: (error) =>

‎apps/mobile/src/connection/storage.ts‎

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "@t3tools/client-runtime/platform";
1111
import { TokenStore } from "@t3tools/client-runtime/authorization";
1212
import {
13+
ConnectionStorageOperationError,
1314
ConnectionTransientError,
1415
CredentialStore,
1516
ProfileStore,
@@ -122,31 +123,40 @@ const secureCatalogStorage: SecureCatalogStorage = {
122123
Effect.tryPromise({
123124
try: () => SecureStore.getItemAsync(key),
124125
catch: (cause) =>
125-
new ConnectionTransientError({
126-
reason: "remote-unavailable",
127-
detail: "Could not load the local connection catalog from secure storage.",
128-
cause,
129-
}),
126+
ConnectionTransientError.fromStorageFailure(
127+
new ConnectionStorageOperationError({
128+
operation: "load",
129+
backend: "mobile-secure-storage",
130+
key,
131+
cause,
132+
}),
133+
),
130134
}),
131135
setItem: (key, value) =>
132136
Effect.tryPromise({
133137
try: () => SecureStore.setItemAsync(key, value),
134138
catch: (cause) =>
135-
new ConnectionTransientError({
136-
reason: "remote-unavailable",
137-
detail: "Could not save the local connection catalog to secure storage.",
138-
cause,
139-
}),
139+
ConnectionTransientError.fromStorageFailure(
140+
new ConnectionStorageOperationError({
141+
operation: "save",
142+
backend: "mobile-secure-storage",
143+
key,
144+
cause,
145+
}),
146+
),
140147
}),
141148
deleteItem: (key) =>
142149
Effect.tryPromise({
143150
try: () => SecureStore.deleteItemAsync(key),
144151
catch: (cause) =>
145-
new ConnectionTransientError({
146-
reason: "remote-unavailable",
147-
detail: "Could not remove the local connection catalog from secure storage.",
148-
cause,
149-
}),
152+
ConnectionTransientError.fromStorageFailure(
153+
new ConnectionStorageOperationError({
154+
operation: "delete",
155+
backend: "mobile-secure-storage",
156+
key,
157+
cause,
158+
}),
159+
),
150160
}),
151161
};
152162

‎apps/web/src/connection/storage.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ describe("makeCatalogBackend", () => {
7171

7272
expect(error).toBeInstanceOf(ConnectionTransientError);
7373
expect(error.message).toContain("Desktop secure storage is unavailable");
74+
expect(error.cause).toMatchObject({ _tag: "DesktopSecureStorageUnavailableError" });
7475
expect(setConnectionCatalog).toHaveBeenCalledWith("{}");
7576
}),
7677
);
@@ -88,10 +89,13 @@ describe("makeCatalogBackend", () => {
8889

8990
const error = yield* backend.write("{}").pipe(Effect.flip);
9091

91-
expect(error.cause).toBe(cause);
92-
expect(error.message).toBe(
93-
"Could not save the local connection catalog to desktop secure storage.",
94-
);
92+
expect(error.cause).toMatchObject({
93+
_tag: "ConnectionStorageOperationError",
94+
operation: "save",
95+
backend: "desktop-secure-storage",
96+
cause,
97+
});
98+
expect(error.message).toBe("Could not save local connection data.");
9599
expect(error.message).not.toContain(cause.message);
96100
}),
97101
);

0 commit comments

Comments
 (0)