error.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { TextField } from "@opencode-ai/ui/text-field"
  2. import { Logo } from "@opencode-ai/ui/logo"
  3. import { Button } from "@opencode-ai/ui/button"
  4. import { Component, Show } from "solid-js"
  5. import { createStore } from "solid-js/store"
  6. import { usePlatform } from "@/context/platform"
  7. import { Icon } from "@opencode-ai/ui/icon"
  8. export type InitError = {
  9. name: string
  10. data: Record<string, unknown>
  11. }
  12. function isInitError(error: unknown): error is InitError {
  13. return (
  14. typeof error === "object" &&
  15. error !== null &&
  16. "name" in error &&
  17. "data" in error &&
  18. typeof (error as InitError).data === "object"
  19. )
  20. }
  21. function safeJson(value: unknown): string {
  22. const seen = new WeakSet<object>()
  23. const json = JSON.stringify(
  24. value,
  25. (_key, val) => {
  26. if (typeof val === "bigint") return val.toString()
  27. if (typeof val === "object" && val) {
  28. if (seen.has(val)) return "[Circular]"
  29. seen.add(val)
  30. }
  31. return val
  32. },
  33. 2,
  34. )
  35. return json ?? String(value)
  36. }
  37. function formatInitError(error: InitError): string {
  38. const data = error.data
  39. switch (error.name) {
  40. case "MCPFailed":
  41. return `MCP server "${data.name}" failed. Note, opencode does not support MCP authentication yet.`
  42. case "ProviderAuthError": {
  43. const providerID = typeof data.providerID === "string" ? data.providerID : "unknown"
  44. const message = typeof data.message === "string" ? data.message : safeJson(data.message)
  45. return `Provider authentication failed (${providerID}): ${message}`
  46. }
  47. case "APIError": {
  48. const message = typeof data.message === "string" ? data.message : "API error"
  49. const lines: string[] = [message]
  50. if (typeof data.statusCode === "number") {
  51. lines.push(`Status: ${data.statusCode}`)
  52. }
  53. if (typeof data.isRetryable === "boolean") {
  54. lines.push(`Retryable: ${data.isRetryable}`)
  55. }
  56. if (typeof data.responseBody === "string" && data.responseBody) {
  57. lines.push(`Response body:\n${data.responseBody}`)
  58. }
  59. return lines.join("\n")
  60. }
  61. case "ProviderModelNotFoundError": {
  62. const { providerID, modelID, suggestions } = data as {
  63. providerID: string
  64. modelID: string
  65. suggestions?: string[]
  66. }
  67. return [
  68. `Model not found: ${providerID}/${modelID}`,
  69. ...(Array.isArray(suggestions) && suggestions.length ? ["Did you mean: " + suggestions.join(", ")] : []),
  70. `Check your config (opencode.json) provider/model names`,
  71. ].join("\n")
  72. }
  73. case "ProviderInitError": {
  74. const providerID = typeof data.providerID === "string" ? data.providerID : "unknown"
  75. return `Failed to initialize provider "${providerID}". Check credentials and configuration.`
  76. }
  77. case "ConfigJsonError": {
  78. const message = typeof data.message === "string" ? data.message : ""
  79. return `Config file at ${data.path} is not valid JSON(C)` + (message ? `: ${message}` : "")
  80. }
  81. case "ConfigDirectoryTypoError":
  82. return `Directory "${data.dir}" in ${data.path} is not valid. Rename the directory to "${data.suggestion}" or remove it. This is a common typo.`
  83. case "ConfigFrontmatterError":
  84. return `Failed to parse frontmatter in ${data.path}:\n${data.message}`
  85. case "ConfigInvalidError": {
  86. const issues = Array.isArray(data.issues)
  87. ? data.issues.map(
  88. (issue: { message: string; path: string[] }) => "↳ " + issue.message + " " + issue.path.join("."),
  89. )
  90. : []
  91. const message = typeof data.message === "string" ? data.message : ""
  92. return [`Config file at ${data.path} is invalid` + (message ? `: ${message}` : ""), ...issues].join("\n")
  93. }
  94. case "UnknownError":
  95. return typeof data.message === "string" ? data.message : safeJson(data)
  96. default:
  97. if (typeof data.message === "string") return data.message
  98. return safeJson(data)
  99. }
  100. }
  101. function formatErrorChain(error: unknown, depth = 0, parentMessage?: string): string {
  102. if (!error) return "Unknown error"
  103. if (isInitError(error)) {
  104. const message = formatInitError(error)
  105. if (depth > 0 && parentMessage === message) return ""
  106. const indent = depth > 0 ? `\n${"─".repeat(40)}\nCaused by:\n` : ""
  107. return indent + `${error.name}\n${message}`
  108. }
  109. if (error instanceof Error) {
  110. const isDuplicate = depth > 0 && parentMessage === error.message
  111. const parts: string[] = []
  112. const indent = depth > 0 ? `\n${"─".repeat(40)}\nCaused by:\n` : ""
  113. const header = `${error.name}${error.message ? `: ${error.message}` : ""}`
  114. const stack = error.stack?.trim()
  115. if (stack) {
  116. const startsWithHeader = stack.startsWith(header)
  117. if (isDuplicate && startsWithHeader) {
  118. const trace = stack.split("\n").slice(1).join("\n").trim()
  119. if (trace) {
  120. parts.push(indent + trace)
  121. }
  122. }
  123. if (isDuplicate && !startsWithHeader) {
  124. parts.push(indent + stack)
  125. }
  126. if (!isDuplicate && startsWithHeader) {
  127. parts.push(indent + stack)
  128. }
  129. if (!isDuplicate && !startsWithHeader) {
  130. parts.push(indent + `${header}\n${stack}`)
  131. }
  132. }
  133. if (!stack && !isDuplicate) {
  134. parts.push(indent + header)
  135. }
  136. if (error.cause) {
  137. const causeResult = formatErrorChain(error.cause, depth + 1, error.message)
  138. if (causeResult) {
  139. parts.push(causeResult)
  140. }
  141. }
  142. return parts.join("\n\n")
  143. }
  144. if (typeof error === "string") {
  145. if (depth > 0 && parentMessage === error) return ""
  146. const indent = depth > 0 ? `\n${"─".repeat(40)}\nCaused by:\n` : ""
  147. return indent + error
  148. }
  149. const indent = depth > 0 ? `\n${"─".repeat(40)}\nCaused by:\n` : ""
  150. return indent + safeJson(error)
  151. }
  152. function formatError(error: unknown): string {
  153. return formatErrorChain(error, 0)
  154. }
  155. interface ErrorPageProps {
  156. error: unknown
  157. }
  158. export const ErrorPage: Component<ErrorPageProps> = (props) => {
  159. const platform = usePlatform()
  160. const [store, setStore] = createStore({
  161. checking: false,
  162. version: undefined as string | undefined,
  163. })
  164. async function checkForUpdates() {
  165. if (!platform.checkUpdate) return
  166. setStore("checking", true)
  167. const result = await platform.checkUpdate()
  168. setStore("checking", false)
  169. if (result.updateAvailable && result.version) setStore("version", result.version)
  170. }
  171. async function installUpdate() {
  172. if (!platform.update || !platform.restart) return
  173. await platform.update()
  174. await platform.restart()
  175. }
  176. return (
  177. <div class="relative flex-1 h-screen w-screen min-h-0 flex flex-col items-center justify-center bg-background-base font-sans">
  178. <div class="w-2/3 max-w-3xl flex flex-col items-center justify-center gap-8">
  179. <Logo class="w-58.5 opacity-12 shrink-0" />
  180. <div class="flex flex-col items-center gap-2 text-center">
  181. <h1 class="text-lg font-medium text-text-strong">Something went wrong</h1>
  182. <p class="text-sm text-text-weak">An error occurred while loading the application.</p>
  183. </div>
  184. <TextField
  185. value={formatError(props.error)}
  186. readOnly
  187. copyable
  188. multiline
  189. class="max-h-96 w-full font-mono text-xs no-scrollbar"
  190. label="Error Details"
  191. hideLabel
  192. />
  193. <div class="flex items-center gap-3">
  194. <Button size="large" onClick={platform.restart}>
  195. Restart
  196. </Button>
  197. <Show when={platform.checkUpdate}>
  198. <Show
  199. when={store.version}
  200. fallback={
  201. <Button size="large" variant="ghost" onClick={checkForUpdates} disabled={store.checking}>
  202. {store.checking ? "Checking..." : "Check for updates"}
  203. </Button>
  204. }
  205. >
  206. <Button size="large" onClick={installUpdate}>
  207. Update to {store.version}
  208. </Button>
  209. </Show>
  210. </Show>
  211. </div>
  212. <div class="flex flex-col items-center gap-2">
  213. <div class="flex items-center justify-center gap-1">
  214. Please report this error to the OpenCode team
  215. <button
  216. type="button"
  217. class="flex items-center text-text-interactive-base gap-1"
  218. onClick={() => platform.openLink("https://opencode.ai/desktop-feedback")}
  219. >
  220. <div>on Discord</div>
  221. <Icon name="discord" class="text-text-interactive-base" />
  222. </button>
  223. </div>
  224. <Show when={platform.version}>
  225. <p class="text-xs text-text-weak">Version: {platform.version}</p>
  226. </Show>
  227. </div>
  228. </div>
  229. </div>
  230. )
  231. }