Compare commits

..
Author SHA1 Message Date
Julius Marmingeandcodex 2b98359c8b [codex] structure legacy connection migration errors
Co-authored-by: codex <[email protected]>
2026-06-20 07:13:45 -07:00
6 changed files with 61 additions and 106 deletions

No files matched your search

@@ -75,4 +75,30 @@ describe("migrateLegacyConnectionCatalog", () => {
expect(catalog.targets).toEqual([]);
}),
);
it.effect("preserves the JSON parse failure", () =>
Effect.gen(function* () {
const error = yield* Effect.flip(migrateLegacyConnectionCatalog("{"));
expect(error).toMatchObject({
_tag: "LegacyConnectionDocumentParseError",
cause: expect.any(SyntaxError),
});
expect(error.message).toBe("Could not parse the legacy mobile connection catalog.");
}),
);
it.effect("preserves the schema decode failure", () =>
Effect.gen(function* () {
const error = yield* Effect.flip(
migrateLegacyConnectionCatalog(JSON.stringify({ connections: "invalid" })),
);
expect(error).toMatchObject({
_tag: "LegacyConnectionDocumentDecodeError",
cause: { _tag: "SchemaError" },
});
expect(error.message).toBe("Could not decode the legacy mobile connection catalog.");
}),
);
});
+19 -16
View File
@@ -33,12 +33,23 @@ const LegacyConnectionDocument = Schema.Struct({
});
const decodeLegacyConnectionDocument = Schema.decodeUnknownEffect(LegacyConnectionDocument);
export class LegacyConnectionMigrationError extends Schema.TaggedErrorClass<LegacyConnectionMigrationError>()(
"LegacyConnectionMigrationError",
{
message: Schema.String,
},
) {}
export class LegacyConnectionDocumentParseError extends Schema.TaggedErrorClass<LegacyConnectionDocumentParseError>()(
"LegacyConnectionDocumentParseError",
{ cause: Schema.Defect() },
) {
override get message(): string {
return "Could not parse the legacy mobile connection catalog.";
}
}
export class LegacyConnectionDocumentDecodeError extends Schema.TaggedErrorClass<LegacyConnectionDocumentDecodeError>()(
"LegacyConnectionDocumentDecodeError",
{ cause: Schema.Defect() },
) {
override get message(): string {
return "Could not decode the legacy mobile connection catalog.";
}
}
function isRelayManaged(connection: typeof LegacySavedRemoteConnection.Type): boolean {
return connection.relayManaged === true || connection.authenticationMethod === "dpop";
@@ -92,18 +103,10 @@ export const migrateLegacyConnectionCatalog = Effect.fn(
)(function* (raw: string) {
const parsed = yield* Effect.try({
try: () => JSON.parse(raw) as unknown,
catch: (cause) =>
new LegacyConnectionMigrationError({
message: `Could not parse the legacy mobile connection catalog: ${String(cause)}`,
}),
catch: (cause) => new LegacyConnectionDocumentParseError({ cause }),
});
const legacy = yield* decodeLegacyConnectionDocument(parsed).pipe(
Effect.mapError(
(cause) =>
new LegacyConnectionMigrationError({
message: `Could not decode the legacy mobile connection catalog: ${String(cause)}`,
}),
),
Effect.mapError((cause) => new LegacyConnectionDocumentDecodeError({ cause })),
);
return (legacy.connections ?? []).reduce(migrateConnection, EMPTY_CONNECTION_CATALOG_DOCUMENT);
@@ -1,12 +1,9 @@
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import { AtomRegistry } from "effect/unstable/reactivity";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import {
createSourceHighlightAtomFamily,
isSourceHighlightError,
type SourceHighlightTokens,
} from "./sourceHighlightingState";
@@ -104,9 +101,8 @@ describe("sourceHighlightingState", () => {
});
it("exposes highlighter errors as a failed async result", async () => {
const cause = new Error("highlight failed");
const highlight = vi.fn(async () => {
throw cause;
throw new Error("highlight failed");
});
const sourceHighlightAtom = createSourceHighlightAtomFamily({ highlight });
const registry = AtomRegistry.make();
@@ -117,17 +113,8 @@ describe("sourceHighlightingState", () => {
});
const unmount = registry.mount(atom);
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
const result = registry.get(atom);
const error = AsyncResult.isFailure(result)
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
: undefined;
expect(isSourceHighlightError(error)).toBe(true);
expect(error).toMatchObject({
path: "src/example.ts",
theme: "light",
cause,
await vi.waitFor(() => {
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
});
unmount();
@@ -1,6 +1,5 @@
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { Atom } from "effect/unstable/reactivity";
import {
@@ -23,20 +22,9 @@ type SourceHighlighter = (input: SourceHighlightInput) => Promise<SourceHighligh
class SourceHighlightCacheKey extends Data.Class<SourceHighlightInput> {}
export class SourceHighlightError extends Schema.TaggedErrorClass<SourceHighlightError>()(
"SourceHighlightError",
{
path: Schema.String,
theme: Schema.Literals(["light", "dark"]),
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Failed to highlight ${this.path} with the ${this.theme} theme.`;
}
}
export const isSourceHighlightError = Schema.is(SourceHighlightError);
class SourceHighlightError extends Data.TaggedError("SourceHighlightError")<{
readonly cause: unknown;
}> {}
export function createSourceHighlightAtomFamily(options?: {
readonly highlight?: SourceHighlighter;
@@ -48,8 +36,7 @@ export function createSourceHighlightAtomFamily(options?: {
Atom.make(
Effect.tryPromise({
try: () => highlight(request),
catch: (cause) =>
new SourceHighlightError({ path: request.path, theme: request.theme, cause }),
catch: (cause) => new SourceHighlightError({ cause }),
}),
).pipe(
Atom.setIdleTTL(idleTtlMs),
@@ -1,13 +1,8 @@
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import { AtomRegistry } from "effect/unstable/reactivity";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import { describe, expect, it, vi } from "vite-plus/test";
import {
createWorkspaceFileImageAtomFamily,
isWorkspaceImagePrefetchError,
} from "./workspace-file-image-cache";
import { createWorkspaceFileImageAtomFamily } from "./workspace-file-image-cache";
describe("workspaceFileImageAtom", () => {
it("reuses a prefetched image across route remounts", async () => {
@@ -53,47 +48,14 @@ describe("workspaceFileImageAtom", () => {
registry.dispose();
});
it("exposes a false native prefetch result", async () => {
it("exposes prefetch failures", async () => {
const imageAtom = createWorkspaceFileImageAtomFamily({ prefetch: async () => false });
const registry = AtomRegistry.make();
const atom = imageAtom("https://example.test/missing.png");
const unmount = registry.mount(atom);
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
const result = registry.get(atom);
const error = AsyncResult.isFailure(result)
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
: undefined;
expect(isWorkspaceImagePrefetchError(error)).toBe(true);
expect(error).toMatchObject({ uri: "https://example.test/missing.png" });
expect(error).not.toHaveProperty("cause");
unmount();
registry.dispose();
});
it("preserves thrown prefetch causes", async () => {
const cause = new Error("native image prefetch failed");
const imageAtom = createWorkspaceFileImageAtomFamily({
prefetch: async () => {
throw cause;
},
});
const registry = AtomRegistry.make();
const atom = imageAtom("https://example.test/missing.png");
const unmount = registry.mount(atom);
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
const result = registry.get(atom);
const error = AsyncResult.isFailure(result)
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
: undefined;
expect(isWorkspaceImagePrefetchError(error)).toBe(true);
expect(error).toMatchObject({
uri: "https://example.test/missing.png",
cause,
await vi.waitFor(() => {
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
});
unmount();
@@ -1,6 +1,5 @@
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { Atom } from "effect/unstable/reactivity";
const WORKSPACE_IMAGE_IDLE_TTL_MS = 30 * 60_000;
@@ -9,19 +8,10 @@ type ImagePrefetch = (uri: string) => Promise<boolean>;
class WorkspaceImageCacheKey extends Data.Class<{ readonly uri: string }> {}
export class WorkspaceImagePrefetchError extends Schema.TaggedErrorClass<WorkspaceImagePrefetchError>()(
"WorkspaceImagePrefetchError",
{
uri: Schema.String,
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return `Failed to prefetch workspace image ${this.uri}.`;
}
}
export const isWorkspaceImagePrefetchError = Schema.is(WorkspaceImagePrefetchError);
export class WorkspaceImagePrefetchError extends Data.TaggedError("WorkspaceImagePrefetchError")<{
readonly cause?: unknown;
readonly uri: string;
}> {}
async function prefetchWithNativeImage(uri: string): Promise<boolean> {
const { Image } = await import("react-native");
@@ -45,7 +35,7 @@ export function createWorkspaceFileImageAtomFamily(options?: {
return key.uri;
},
catch: (cause) =>
isWorkspaceImagePrefetchError(cause)
cause instanceof WorkspaceImagePrefetchError
? cause
: new WorkspaceImagePrefetchError({ uri: key.uri, cause }),
}),