From be6e7b309eee2f53a3913db9ef4ba43132b9287d Mon Sep 17 00:00:00 2001 From: Shoubhit Dash Date: Thu, 14 May 2026 14:48:58 +0530 Subject: [PATCH] refactor(provider): type init errors (#27484) --- packages/opencode/src/cli/error.ts | 5 +++-- packages/opencode/src/provider/provider.ts | 18 +++++++++++------- packages/opencode/test/cli/error.test.ts | 8 ++++++++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/opencode/src/cli/error.ts b/packages/opencode/src/cli/error.ts index ffb0e0cfba..c92369b0af 100644 --- a/packages/opencode/src/cli/error.ts +++ b/packages/opencode/src/cli/error.ts @@ -72,8 +72,9 @@ export function FormatError(input: unknown) { } // ProviderInitError: { providerID: string } - if (NamedError.hasName(input, "ProviderInitError")) { - return `Failed to initialize provider "${(input as ErrorLike).data?.providerID}". Check credentials and configuration.` + const providerInit = configData(input, "ProviderInitError") + if (providerInit) { + return `Failed to initialize provider "${stringField(providerInit, "providerID")}". Check credentials and configuration.` } // ConfigJsonError: { path: string, message?: string } diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index 09dc40bc42..6401518c7e 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -13,7 +13,6 @@ import { Auth } from "../auth" import { Env } from "../env" import { InstallationVersion } from "@opencode-ai/core/installation/version" import { Flag } from "@opencode-ai/core/flag/flag" -import { NamedError } from "@opencode-ai/core/util/error" import { iife } from "@/util/iife" import { Global } from "@opencode-ai/core/global" import path from "path" @@ -975,7 +974,16 @@ export class ModelNotFoundError extends Schema.TaggedErrorClass()("ProviderInitError", { + providerID: ProviderID, + cause: Schema.optional(Schema.Defect), +}) { + static isInstance(input: unknown): input is InitError { + return input instanceof InitError + } +} + +export type Error = ModelNotFoundError | InitError export interface Interface { readonly list: () => Effect.Effect> @@ -1634,7 +1642,7 @@ const layer = Layer.effect( s.sdk.set(key, loaded) return loaded as SDK } catch (e) { - throw new InitError({ providerID: model.providerID }, { cause: e }) + throw new InitError({ providerID: model.providerID, cause: e }) } } @@ -1827,8 +1835,4 @@ export function parseModel(model: string) { } } -export const InitError = NamedError.create("ProviderInitError", { - providerID: ProviderID, -}) - export * as Provider from "./provider" diff --git a/packages/opencode/test/cli/error.test.ts b/packages/opencode/test/cli/error.test.ts index 63e04695d1..b29ca2b3ba 100644 --- a/packages/opencode/test/cli/error.test.ts +++ b/packages/opencode/test/cli/error.test.ts @@ -81,6 +81,14 @@ describe("cli.error", () => { expect(FormatError({ _tag: "ProviderModelNotFoundError", ...data })).toBe(expected) }) + test("formats legacy and tagged provider init errors the same way", () => { + const data = { providerID: "anthropic" } + const expected = 'Failed to initialize provider "anthropic". Check credentials and configuration.' + + expect(FormatError({ name: "ProviderInitError", data })).toBe(expected) + expect(FormatError({ _tag: "ProviderInitError", ...data })).toBe(expected) + }) + test("formats cancelled UI errors as empty output", () => { expect(FormatError(new UI.CancelledError())).toBe("") })