Skip to content

Commit be6e7b3

Browse files
authored
refactor(provider): type init errors (anomalyco#27484)
1 parent 0af2429 commit be6e7b3

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

packages/opencode/src/cli/error.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ export function FormatError(input: unknown) {
7272
}
7373

7474
// ProviderInitError: { providerID: string }
75-
if (NamedError.hasName(input, "ProviderInitError")) {
76-
return `Failed to initialize provider "${(input as ErrorLike).data?.providerID}". Check credentials and configuration.`
75+
const providerInit = configData(input, "ProviderInitError")
76+
if (providerInit) {
77+
return `Failed to initialize provider "${stringField(providerInit, "providerID")}". Check credentials and configuration.`
7778
}
7879

7980
// ConfigJsonError: { path: string, message?: string }

packages/opencode/src/provider/provider.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { Auth } from "../auth"
1313
import { Env } from "../env"
1414
import { InstallationVersion } from "@opencode-ai/core/installation/version"
1515
import { Flag } from "@opencode-ai/core/flag/flag"
16-
import { NamedError } from "@opencode-ai/core/util/error"
1716
import { iife } from "@/util/iife"
1817
import { Global } from "@opencode-ai/core/global"
1918
import path from "path"
@@ -975,7 +974,16 @@ export class ModelNotFoundError extends Schema.TaggedErrorClass<ModelNotFoundErr
975974
}
976975
}
977976

978-
export type Error = ModelNotFoundError
977+
export class InitError extends Schema.TaggedErrorClass<InitError>()("ProviderInitError", {
978+
providerID: ProviderID,
979+
cause: Schema.optional(Schema.Defect),
980+
}) {
981+
static isInstance(input: unknown): input is InitError {
982+
return input instanceof InitError
983+
}
984+
}
985+
986+
export type Error = ModelNotFoundError | InitError
979987

980988
export interface Interface {
981989
readonly list: () => Effect.Effect<Record<ProviderID, Info>>
@@ -1634,7 +1642,7 @@ const layer = Layer.effect(
16341642
s.sdk.set(key, loaded)
16351643
return loaded as SDK
16361644
} catch (e) {
1637-
throw new InitError({ providerID: model.providerID }, { cause: e })
1645+
throw new InitError({ providerID: model.providerID, cause: e })
16381646
}
16391647
}
16401648

@@ -1827,8 +1835,4 @@ export function parseModel(model: string) {
18271835
}
18281836
}
18291837

1830-
export const InitError = NamedError.create("ProviderInitError", {
1831-
providerID: ProviderID,
1832-
})
1833-
18341838
export * as Provider from "./provider"

packages/opencode/test/cli/error.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ describe("cli.error", () => {
8181
expect(FormatError({ _tag: "ProviderModelNotFoundError", ...data })).toBe(expected)
8282
})
8383

84+
test("formats legacy and tagged provider init errors the same way", () => {
85+
const data = { providerID: "anthropic" }
86+
const expected = 'Failed to initialize provider "anthropic". Check credentials and configuration.'
87+
88+
expect(FormatError({ name: "ProviderInitError", data })).toBe(expected)
89+
expect(FormatError({ _tag: "ProviderInitError", ...data })).toBe(expected)
90+
})
91+
8492
test("formats cancelled UI errors as empty output", () => {
8593
expect(FormatError(new UI.CancelledError())).toBe("")
8694
})

0 commit comments

Comments
 (0)