This repository was archived by the owner on Mar 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathmods_errors.go
More file actions
70 lines (65 loc) · 1.92 KB
/
mods_errors.go
File metadata and controls
70 lines (65 loc) · 1.92 KB
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
package main
import (
"errors"
"fmt"
"net/http"
tea "github.com/charmbracelet/bubbletea"
"github.com/openai/openai-go"
)
func (m *Mods) handleRequestError(err error, mod Model, content string) tea.Msg {
ae := &openai.Error{}
if errors.As(err, &ae) {
return m.handleAPIError(ae, mod, content)
}
return modsError{err, fmt.Sprintf(
"There was a problem with the %s API request.",
mod.API,
)}
}
func (m *Mods) handleAPIError(err *openai.Error, mod Model, content string) tea.Msg {
cfg := m.Config
switch err.StatusCode {
case http.StatusNotFound:
if mod.Fallback != "" {
m.Config.Model = mod.Fallback
return m.retry(content, modsError{
err: err,
reason: fmt.Sprintf("%s API server error.", mod.API),
})
}
return modsError{err: err, reason: fmt.Sprintf(
"Missing model '%s' for API '%s'.",
cfg.Model,
cfg.API,
)}
case http.StatusBadRequest:
if err.Code == "context_length_exceeded" {
pe := modsError{err: err, reason: "Maximum prompt size exceeded."}
if cfg.NoLimit {
return pe
}
return m.retry(cutPrompt(err.Message, content), pe)
}
// bad request (do not retry)
return modsError{err: err, reason: fmt.Sprintf("%s API request error.", mod.API)}
case http.StatusUnauthorized:
// invalid auth or key (do not retry)
return modsError{err: err, reason: fmt.Sprintf("Invalid %s API key.", mod.API)}
case http.StatusTooManyRequests:
// rate limiting or engine overload (wait and retry)
return m.retry(content, modsError{
err: err, reason: fmt.Sprintf("You’ve hit your %s API rate limit.", mod.API),
})
case http.StatusInternalServerError:
if mod.API == "openai" {
return m.retry(content, modsError{err: err, reason: "OpenAI API server error."})
}
return modsError{err: err, reason: fmt.Sprintf(
"Error loading model '%s' for API '%s'.",
mod.Name,
mod.API,
)}
default:
return m.retry(content, modsError{err: err, reason: "Unknown API error."})
}
}