summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/utils/server-errors.test.ts
blob: 1969d1afc271bacedab981e11eadbd56f2c7ee88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { describe, expect, test } from "bun:test"
import type { ConfigInvalidError } from "./server-errors"
import { formatServerError, parseReabaleConfigInvalidError } from "./server-errors"

describe("parseReabaleConfigInvalidError", () => {
  test("formats issues with file path", () => {
    const error = {
      name: "ConfigInvalidError",
      data: {
        path: "opencode.config.ts",
        issues: [
          { path: ["settings", "host"], message: "Required" },
          { path: ["mode"], message: "Invalid" },
        ],
      },
    } satisfies ConfigInvalidError

    const result = parseReabaleConfigInvalidError(error)

    expect(result).toBe(
      ["Invalid configuration", "opencode.config.ts", "settings.host: Required", "mode: Invalid"].join("\n"),
    )
  })

  test("uses trimmed message when issues are missing", () => {
    const error = {
      name: "ConfigInvalidError",
      data: {
        path: "config",
        message: "  Bad value  ",
      },
    } satisfies ConfigInvalidError

    const result = parseReabaleConfigInvalidError(error)

    expect(result).toBe(["Invalid configuration", "Bad value"].join("\n"))
  })
})

describe("formatServerError", () => {
  test("formats config invalid errors", () => {
    const error = {
      name: "ConfigInvalidError",
      data: {
        message: "Missing host",
      },
    } satisfies ConfigInvalidError

    const result = formatServerError(error)

    expect(result).toBe(["Invalid configuration", "Missing host"].join("\n"))
  })

  test("returns error messages", () => {
    expect(formatServerError(new Error("Request failed with status 503"))).toBe("Request failed with status 503")
  })

  test("returns provided string errors", () => {
    expect(formatServerError("Failed to connect to server")).toBe("Failed to connect to server")
  })

  test("falls back to unknown", () => {
    expect(formatServerError(0)).toBe("Unknown error")
  })

  test("falls back for unknown error objects and names", () => {
    expect(formatServerError({ name: "ServerTimeoutError", data: { seconds: 30 } })).toBe("Unknown error")
  })
})