File size: 7,025 Bytes
0dbc9de | 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import { isRecord } from "./record"
type ConfigIssue = { message: string; path: string[] }
export function cliErrorMessage(input: unknown): string | undefined {
if (input instanceof Error && isRecord(input.cause) && "body" in input.cause) {
const formatted = cliErrorMessage(input.cause.body)
if (formatted) return formatted
}
if (tagged(input, "CliError")) {
if (typeof input.exitCode === "number") process.exitCode = input.exitCode
return field(input, "message") ?? ""
}
if (tagged(input, "AccountServiceError") || tagged(input, "AccountTransportError")) {
return field(input, "message") ?? ""
}
const model = configData(input, "ProviderModelNotFoundError")
if (model) {
const suggestions = Array.isArray(model.suggestions)
? model.suggestions.filter((item): item is string => typeof item === "string")
: []
return [
`Model not found: ${field(model, "providerID")}/${field(model, "modelID")}`,
...(suggestions.length ? ["Did you mean: " + suggestions.join(", ")] : []),
"Try: `opencode models` to list available models",
"Or check your config (opencode.json) provider/model names",
].join("\n")
}
const provider = configData(input, "ProviderInitError")
if (provider)
return `Failed to initialize provider "${field(provider, "providerID")}". Check credentials and configuration.`
const json = configData(input, "ConfigJsonError")
if (json) {
const message = field(json, "message")
return `Config file at ${field(json, "path")} is not valid JSON(C)` + (message ? `: ${message}` : "")
}
const directory = configData(input, "ConfigDirectoryTypoError")
if (directory) {
return `Directory "${field(directory, "dir")}" in ${field(directory, "path")} is not valid. Rename the directory to "${field(directory, "suggestion")}" or remove it. This is a common typo.`
}
const frontmatter = configData(input, "ConfigFrontmatterError")
if (frontmatter) return field(frontmatter, "message") ?? ""
const remoteAuth = configData(input, "ConfigRemoteAuthError")
if (remoteAuth) {
const url = field(remoteAuth, "url")
const remote = field(remoteAuth, "remote")
return [
`Failed to load remote config${remote ? ` from ${remote}` : ""}: the server returned a login page instead of JSON.`,
"Authentication is missing or has expired (the endpoint is likely behind an SSO or identity-aware proxy).",
...(url ? [`Run \`opencode auth login ${url}\` to re-authenticate.`] : []),
].join("\n")
}
const invalid = configData(input, "ConfigInvalidError")
if (invalid) {
const path = field(invalid, "path")
const message = field(invalid, "message")
const issues = Array.isArray(invalid.issues)
? invalid.issues.filter((issue): issue is ConfigIssue => {
return (
isRecord(issue) &&
typeof issue.message === "string" &&
Array.isArray(issue.path) &&
issue.path.every((item) => typeof item === "string")
)
})
: []
return [
`Configuration is invalid${path && path !== "config" ? ` at ${path}` : ""}` + (message ? `: ${message}` : ""),
...issues.map((issue) => "↳ " + issue.message + " " + issue.path.join(".")),
].join("\n")
}
if (tagged(input, "UICancelledError") || named(input, "UICancelledError")) return ""
if (isRecord(input) && named(input, "MCPFailed")) {
const name = isRecord(input.data) ? field(input.data, "name") : undefined
return `MCP server "${name}" failed. Note, opencode does not support MCP authentication yet.`
}
return undefined
}
function tagged(input: unknown, tag: string): input is Record<string, unknown> {
return isRecord(input) && input._tag === tag
}
function named(input: unknown, name: string) {
return isRecord(input) && (input.name === name || input._tag === name)
}
function configData(input: unknown, tag: string) {
if (!isRecord(input)) return undefined
if (input.name === tag && isRecord(input.data)) return input.data
if (input._tag === tag) return input
return undefined
}
function field(input: Record<string, unknown>, key: string) {
return typeof input[key] === "string" ? input[key] : undefined
}
export function errorFormat(error: unknown): string {
if (error instanceof Error) {
return error.stack ?? `${error.name}: ${error.message}`
}
if (typeof error === "object" && error !== null) {
try {
const json = JSON.stringify(error, null, 2)
// Plain objects whose own properties are all non-enumerable (or empty)
// serialize to "{}", which prints as a useless bare `{}` on stderr.
// Fall back to a custom toString first, then to ctor name + own prop names.
if (json === "{}") {
const str = String(error)
if (str && str !== "[object Object]") return str
const ctor = error.constructor?.name
const prefix = ctor && ctor !== "Object" ? ctor : "Error"
const names = Object.getOwnPropertyNames(error)
return names.length === 0 ? `${prefix} (no message)` : `${prefix} { ${names.join(", ")} }`
}
return json
} catch {
return "Unexpected error (unserializable)"
}
}
return String(error)
}
export function errorMessage(error: unknown): string {
if (error instanceof Error) {
if (error.message) return error.message
if (error.name) return error.name
}
if (isRecord(error) && typeof error.message === "string" && error.message) {
return error.message
}
if (isRecord(error) && isRecord(error.data) && typeof error.data.message === "string" && error.data.message) {
return error.data.message
}
const text = String(error)
if (text && text !== "[object Object]") return text
const formatted = errorFormat(error)
if (formatted) return formatted
return "unknown error"
}
export function errorData(error: unknown) {
if (error instanceof Error) {
return {
type: error.name,
message: errorMessage(error),
stack: error.stack,
cause: error.cause === undefined ? undefined : errorFormat(error.cause),
formatted: errorFormat(error),
}
}
if (!isRecord(error)) {
return {
type: typeof error,
message: errorMessage(error),
formatted: errorFormat(error),
}
}
const data = Object.getOwnPropertyNames(error).reduce<Record<string, unknown>>((acc, key) => {
const value = error[key]
if (value === undefined) return acc
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
acc[key] = value
return acc
}
// oxlint-disable-next-line no-base-to-string -- intentional coercion of arbitrary error properties
acc[key] = value instanceof Error ? value.message : String(value)
return acc
}, {})
if (typeof data.message !== "string") data.message = errorMessage(error)
if (typeof data.type !== "string") data.type = error.constructor?.name
data.formatted = errorFormat(error)
return data
}
|