-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
78 lines (71 loc) · 1.84 KB
/
Copy patherrors.go
File metadata and controls
78 lines (71 loc) · 1.84 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
71
72
73
74
75
76
77
78
package codex
import (
"bytes"
"encoding/json"
"errors"
"strings"
"github.com/pmenglund/codex-sdk-go/rpc"
)
// ErrOverloaded identifies retryable overload or server-busy failures.
var ErrOverloaded = errors.New("codex overloaded")
// IsOverloaded reports whether err indicates an overload or server-busy failure.
func IsOverloaded(err error) bool {
if err == nil {
return false
}
if errors.Is(err, ErrOverloaded) {
return true
}
var responseErr *rpc.ResponseError
if errors.As(err, &responseErr) {
if responseErr.Detail.Code == 429 {
return true
}
if containsOverloadText(responseErr.Detail.Message) || containsOverloadText(string(responseErr.Detail.Data)) {
return true
}
if responseErr.Detail.Data != nil && json.Valid(responseErr.Detail.Data) {
var payload any
if json.Unmarshal(responseErr.Detail.Data, &payload) == nil && containsOverloadJSON(payload) {
return true
}
}
}
return containsOverloadText(err.Error())
}
// IsRetryable reports whether err is safe for SDK callers to retry.
func IsRetryable(err error) bool {
return IsOverloaded(err)
}
func containsOverloadJSON(value any) bool {
switch value := value.(type) {
case string:
return containsOverloadText(value)
case []any:
for _, item := range value {
if containsOverloadJSON(item) {
return true
}
}
case map[string]any:
for key, item := range value {
if containsOverloadText(key) || containsOverloadJSON(item) {
return true
}
}
}
return false
}
func containsOverloadText(value string) bool {
value = strings.ToLower(value)
value = string(bytes.Map(func(r rune) rune {
if r == '-' || r == '_' {
return ' '
}
return r
}, []byte(value)))
return strings.Contains(value, "overload") ||
strings.Contains(value, "server busy") ||
strings.Contains(value, "too many requests") ||
strings.Contains(value, "rate limit")
}