Skip to content

Commit cc02476

Browse files
authored
refactor: replace error handling with serverErrorMessage utility and checks for if error is ConfigInvalidError (anomalyco#14685)
1 parent 5190589 commit cc02476

4 files changed

Lines changed: 110 additions & 8 deletions

File tree

packages/app/src/context/global-sync.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import type { ProjectMeta } from "./global-sync/types"
3636
import { SESSION_RECENT_LIMIT } from "./global-sync/types"
3737
import { sanitizeProject } from "./global-sync/utils"
3838
import { usePlatform } from "./platform"
39+
import { formatServerError } from "@/utils/server-errors"
3940

4041
type GlobalStore = {
4142
ready: boolean
@@ -51,11 +52,6 @@ type GlobalStore = {
5152
reload: undefined | "pending" | "complete"
5253
}
5354

54-
function errorMessage(error: unknown) {
55-
if (error instanceof Error && error.message) return error.message
56-
if (typeof error === "string" && error) return error
57-
return "Unknown error"
58-
}
5955

6056
function createGlobalSync() {
6157
const globalSDK = useGlobalSDK()
@@ -207,8 +203,9 @@ function createGlobalSync() {
207203
console.error("Failed to load sessions", err)
208204
const project = getFilename(directory)
209205
showToast({
206+
variant: "error",
210207
title: language.t("toast.session.listFailed.title", { project }),
211-
description: errorMessage(err),
208+
description: formatServerError(err),
212209
})
213210
})
214211

packages/app/src/context/global-sync/bootstrap.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { batch } from "solid-js"
1616
import { reconcile, type SetStoreFunction, type Store } from "solid-js/store"
1717
import type { State, VcsCache } from "./types"
1818
import { cmp, normalizeProviderList } from "./utils"
19+
import { formatServerError } from "@/utils/server-errors"
1920

2021
type GlobalStore = {
2122
ready: boolean
@@ -133,8 +134,11 @@ export async function bootstrapDirectory(input: {
133134
} catch (err) {
134135
console.error("Failed to bootstrap instance", err)
135136
const project = getFilename(input.directory)
136-
const message = err instanceof Error ? err.message : String(err)
137-
showToast({ title: `Failed to reload ${project}`, description: message })
137+
showToast({
138+
variant: "error",
139+
title: `Failed to reload ${project}`,
140+
description: formatServerError(err)
141+
})
138142
input.setStore("status", "partial")
139143
return
140144
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { describe, expect, test } from "bun:test"
2+
import type { ConfigInvalidError } from "./server-errors"
3+
import { formatServerError, parseReabaleConfigInvalidError } from "./server-errors"
4+
5+
describe("parseReabaleConfigInvalidError", () => {
6+
test("formats issues with file path", () => {
7+
const error = {
8+
name: "ConfigInvalidError",
9+
data: {
10+
path: "opencode.config.ts",
11+
issues: [
12+
{ path: ["settings", "host"], message: "Required" },
13+
{ path: ["mode"], message: "Invalid" },
14+
],
15+
},
16+
} satisfies ConfigInvalidError
17+
18+
const result = parseReabaleConfigInvalidError(error)
19+
20+
expect(result).toBe(
21+
["Invalid configuration", "opencode.config.ts", "settings.host: Required", "mode: Invalid"].join("\n"),
22+
)
23+
})
24+
25+
test("uses trimmed message when issues are missing", () => {
26+
const error = {
27+
name: "ConfigInvalidError",
28+
data: {
29+
path: "config",
30+
message: " Bad value ",
31+
},
32+
} satisfies ConfigInvalidError
33+
34+
const result = parseReabaleConfigInvalidError(error)
35+
36+
expect(result).toBe(["Invalid configuration", "Bad value"].join("\n"))
37+
})
38+
})
39+
40+
describe("formatServerError", () => {
41+
test("formats config invalid errors", () => {
42+
const error = {
43+
name: "ConfigInvalidError",
44+
data: {
45+
message: "Missing host",
46+
},
47+
} satisfies ConfigInvalidError
48+
49+
const result = formatServerError(error)
50+
51+
expect(result).toBe(["Invalid configuration", "Missing host"].join("\n"))
52+
})
53+
54+
test("returns error messages", () => {
55+
expect(formatServerError(new Error("Request failed with status 503"))).toBe("Request failed with status 503")
56+
})
57+
58+
test("returns provided string errors", () => {
59+
expect(formatServerError("Failed to connect to server")).toBe("Failed to connect to server")
60+
})
61+
62+
test("falls back to unknown", () => {
63+
expect(formatServerError(0)).toBe("Unknown error")
64+
})
65+
66+
test("falls back for unknown error objects and names", () => {
67+
expect(formatServerError({ name: "ServerTimeoutError", data: { seconds: 30 } })).toBe("Unknown error")
68+
})
69+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export type ConfigInvalidError = {
2+
name: "ConfigInvalidError"
3+
data: {
4+
path?: string
5+
message?: string
6+
issues?: Array<{ message: string; path: string[] }>
7+
}
8+
}
9+
10+
export function formatServerError(error: unknown) {
11+
if (isConfigInvalidErrorLike(error)) return parseReabaleConfigInvalidError(error)
12+
if (error instanceof Error && error.message) return error.message
13+
if (typeof error === "string" && error) return error
14+
return "Unknown error"
15+
}
16+
17+
function isConfigInvalidErrorLike(error: unknown): error is ConfigInvalidError {
18+
if (typeof error !== "object" || error === null) return false
19+
const o = error as Record<string, unknown>
20+
return o.name === "ConfigInvalidError" && typeof o.data === "object" && o.data !== null
21+
}
22+
23+
export function parseReabaleConfigInvalidError(errorInput: ConfigInvalidError) {
24+
const head = "Invalid configuration"
25+
const file = errorInput.data.path && errorInput.data.path !== "config" ? errorInput.data.path : ""
26+
const detail = errorInput.data.message?.trim() ?? ""
27+
const issues = (errorInput.data.issues ?? []).map((issue) => {
28+
return `${issue.path.join(".")}: ${issue.message}`
29+
})
30+
if (issues.length) return [head, file, "", ...issues].filter(Boolean).join("\n")
31+
return [head, file, detail].filter(Boolean).join("\n")
32+
}

0 commit comments

Comments
 (0)