2025-10-30 23:56:30 +08:00
import { ConfigMarkdown } from "@/config/markdown"
2025-06-20 12:57:28 +08:00
import { Config } from "../config/config"
2025-06-23 09:10:05 +08:00
import { MCP } from "../mcp"
2025-11-21 16:01:19 +08:00
import { Provider } from "../provider/provider"
2025-06-25 10:09:43 +08:00
import { UI } from "./ui"
2025-06-20 12:57:28 +08:00
export function FormatError ( input : unknown ) {
2025-06-23 09:10:05 +08:00
if ( MCP . Failed . isInstance ( input ) )
return ` MCP server " ${ input . data . name } " failed. Note, opencode does not support MCP authentication yet. `
2025-11-21 16:01:19 +08:00
if ( Provider . ModelNotFoundError . isInstance ( input ) ) {
const { providerID , modelID , suggestions } = input . data
return [
` Model not found: ${ providerID } / ${ modelID } ` ,
. . . ( Array . isArray ( suggestions ) && 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" )
}
if ( Provider . InitError . isInstance ( input ) ) {
return ` Failed to initialize provider " ${ input . data . providerID } ". Check credentials and configuration. `
}
2025-07-31 19:25:26 +08:00
if ( Config . JsonError . isInstance ( input ) ) {
return (
2025-11-08 09:59:02 +08:00
` Config file at ${ input . data . path } is not valid JSON(C) ` + ( input . data . message ? ` : ${ input . data . message } ` : "" )
2025-07-31 19:25:26 +08:00
)
}
2025-10-18 03:34:37 +08:00
if ( Config . ConfigDirectoryTypoError . isInstance ( input ) ) {
2025-11-21 12:36:07 +08:00
return ` Directory " ${ input . data . dir } " in ${ input . data . path } is not valid. Rename the directory to " ${ input . data . suggestion } " or remove it. This is a common typo. `
2025-10-18 03:34:37 +08:00
}
2025-10-30 23:56:30 +08:00
if ( ConfigMarkdown . FrontmatterError . isInstance ( input ) ) {
return ` Failed to parse frontmatter in ${ input . data . path } : \ n ${ input . data . message } `
}
2025-06-20 12:57:28 +08:00
if ( Config . InvalidError . isInstance ( input ) )
return [
2025-12-21 03:47:28 +08:00
` Configuration is invalid ${ input . data . path && input . data . path !== "config" ? ` at ${ input . data . path } ` : "" } ` +
( input . data . message ? ` : ${ input . data . message } ` : "" ) ,
2025-11-08 09:59:02 +08:00
. . . ( input . data . issues ? . map ( ( issue ) = > "↳ " + issue . message + " " + issue . path . join ( "." ) ) ? ? [ ] ) ,
2025-06-20 12:57:28 +08:00
] . join ( "\n" )
2025-06-25 10:09:43 +08:00
if ( UI . CancelledError . isInstance ( input ) ) return ""
2025-06-20 12:57:28 +08:00
}
2025-11-27 00:49:55 +08:00
export function FormatUnknownError ( input : unknown ) : string {
if ( input instanceof Error ) {
return input . stack ? ? ` ${ input . name } : ${ input . message } `
}
if ( typeof input === "object" && input !== null ) {
try {
const json = JSON . stringify ( input , null , 2 )
if ( json && json !== "{}" ) return json
} catch { }
}
return String ( input )
}