Files
coder/coderd/x/chatd/chaterror/message.go
T
Ethan 4751416b29 fix!: persist structured chat errors (#24919)
**Breaking change for changelog:**

> `codersdk.Chat.last_error` now returns a structured `ChatError` object
(`{message, kind, provider, retryable, status_code, detail}`) instead of
a plain string. The chats API is experimental
(`/api/experimental/chats`), so this ships without a deprecation cycle;
consumers reading `chat.last_error` as a string must update to read
`chat.last_error.message`. SDK/generated TypeScript terminal error
payloads now use the single `ChatError` type; the live stream error
payload type is renamed from `ChatStreamError` to `ChatError`.

Persisted chat errors now carry the same provider-specific detail (kind,
provider, retryable, HTTP status, optional detail) as the live stream,
so refreshing a failed chat rehydrates with the full structured error
instead of a one-line headline.

Existing rows are migrated in place: legacy text errors are wrapped into
`{message, kind: "generic"}` so already-errored chats still render, and
rows with `last_error IS NULL` stay NULL. Internally, persisted fallback
decoding now reuses the existing `chaterror.KindGeneric` constant, with
no JSON value change.

Closes CODAGT-239
2026-05-05 12:56:06 +10:00

136 lines
3.6 KiB
Go

package chaterror
import (
"fmt"
"strings"
)
// terminalMessage produces the user-facing error description shown
// when retries are exhausted. HTTP status codes are carried in the
// classified payload's StatusCode field and rendered as a separate
// footer chip by the UI, so they are intentionally omitted here to
// avoid duplicating the same information in two places.
func terminalMessage(classified ClassifiedError) string {
subject := providerSubject(classified.Provider)
switch classified.Kind {
case KindOverloaded:
return fmt.Sprintf("%s is temporarily overloaded.", subject)
case KindRateLimit:
return fmt.Sprintf("%s is rate limiting requests.", subject)
case KindTimeout:
if !classified.Retryable && classified.StatusCode == 0 {
return "The request timed out before it completed."
}
return fmt.Sprintf("%s is temporarily unavailable.", subject)
case KindStartupTimeout:
return fmt.Sprintf(
"%s did not start responding in time.", subject,
)
case KindAuth:
displayName := providerDisplayName(classified.Provider)
if displayName == "" {
displayName = "the AI provider"
}
return fmt.Sprintf(
"Authentication with %s failed."+
" Check the API key, permissions, and billing settings.",
displayName,
)
case KindConfig:
return fmt.Sprintf(
"%s rejected the model configuration."+
" Check the selected model and provider settings.",
subject,
)
default:
if !classified.Retryable && classified.StatusCode == 0 {
return "The chat request failed unexpectedly."
}
return fmt.Sprintf("%s returned an unexpected error.", subject)
}
}
// retryMessage produces a clean factual description suitable for
// display alongside the retry countdown UI. It omits HTTP status
// codes (surfaced separately in the payload) and remediation
// guidance (not actionable while auto-retrying).
func retryMessage(classified ClassifiedError) string {
subject := providerSubject(classified.Provider)
switch classified.Kind {
case KindOverloaded:
return fmt.Sprintf("%s is temporarily overloaded.", subject)
case KindRateLimit:
return fmt.Sprintf("%s is rate limiting requests.", subject)
case KindTimeout:
return fmt.Sprintf("%s is temporarily unavailable.", subject)
case KindStartupTimeout:
return fmt.Sprintf(
"%s did not start responding in time.", subject,
)
case KindAuth:
displayName := providerDisplayName(classified.Provider)
if displayName == "" {
displayName = "the AI provider"
}
return fmt.Sprintf(
"Authentication with %s failed.", displayName,
)
case KindConfig:
return fmt.Sprintf(
"%s rejected the model configuration.", subject,
)
default:
return fmt.Sprintf(
"%s returned an unexpected error.", subject,
)
}
}
func providerSubject(provider string) string {
if displayName := providerDisplayName(provider); displayName != "" {
return displayName
}
return "The AI provider"
}
func providerDisplayName(provider string) string {
switch normalizeProvider(provider) {
case "anthropic":
return "Anthropic"
case "azure":
return "Azure OpenAI"
case "bedrock":
return "AWS Bedrock"
case "google":
return "Google"
case "openai":
return "OpenAI"
case "openai-compat":
return "OpenAI Compatible"
case "openrouter":
return "OpenRouter"
case "vercel":
return "Vercel AI Gateway"
default:
return ""
}
}
func normalizeProvider(provider string) string {
normalized := strings.ToLower(strings.TrimSpace(provider))
switch normalized {
case "azure openai", "azure-openai":
return "azure"
case "openai compat", "openai compatible", "openai_compat":
return "openai-compat"
default:
return normalized
}
}