-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
169 lines (150 loc) · 4.22 KB
/
errors.go
File metadata and controls
169 lines (150 loc) · 4.22 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package runagent
import (
"fmt"
"strings"
)
// ErrorType captures the standardized error taxonomy shared across SDKs.
type ErrorType string
const (
ErrorTypeAuthentication ErrorType = "AUTHENTICATION_ERROR"
ErrorTypePermission ErrorType = "PERMISSION_ERROR"
ErrorTypeConnection ErrorType = "CONNECTION_ERROR"
ErrorTypeValidation ErrorType = "VALIDATION_ERROR"
ErrorTypeServer ErrorType = "SERVER_ERROR"
ErrorTypeUnknown ErrorType = "UNKNOWN_ERROR"
)
// RunAgentError is the root error type returned by the Go SDK.
type RunAgentError struct {
Type ErrorType
Code string
Message string
Suggestion string
Details map[string]interface{}
Cause error
}
func (e *RunAgentError) Error() string {
if e == nil {
return "<nil>"
}
base := fmt.Sprintf("%s: %s", e.Type, e.Message)
if e.Code != "" {
base = fmt.Sprintf("%s (%s)", base, e.Code)
}
if e.Suggestion != "" {
base = fmt.Sprintf("%s | suggestion: %s", base, e.Suggestion)
}
return base
}
// Unwrap exposes the wrapped cause when available.
func (e *RunAgentError) Unwrap() error {
if e == nil {
return nil
}
return e.Cause
}
// RunAgentExecutionError represents errors returned by the RunAgent service.
type RunAgentExecutionError struct {
*RunAgentError
HTTPStatus int
}
func newError(kind ErrorType, message string, opts ...func(*RunAgentError)) *RunAgentError {
err := &RunAgentError{
Type: kind,
Message: message,
}
for _, opt := range opts {
opt(err)
}
return err
}
func withCode(code string) func(*RunAgentError) {
return func(e *RunAgentError) {
e.Code = code
}
}
func withSuggestion(s string) func(*RunAgentError) {
return func(e *RunAgentError) {
e.Suggestion = s
}
}
func withDetails(details map[string]interface{}) func(*RunAgentError) {
return func(e *RunAgentError) {
e.Details = details
}
}
func withCause(err error) func(*RunAgentError) {
return func(e *RunAgentError) {
e.Cause = err
}
}
func newExecutionError(status int, apiErr *apiErrorPayload) *RunAgentExecutionError {
if apiErr == nil {
apiErr = &apiErrorPayload{
Type: ErrorTypeUnknown,
Message: "agent execution failed",
}
}
apiErr = enrichErrorPayload(apiErr)
runErr := &RunAgentError{
Type: apiErr.Type,
Code: apiErr.Code,
Message: apiErr.Message,
Suggestion: apiErr.Suggestion,
Details: apiErr.Details,
}
if runErr.Type == "" {
runErr.Type = ErrorTypeServer
}
return &RunAgentExecutionError{
RunAgentError: runErr,
HTTPStatus: status,
}
}
// enrichErrorPayload adds friendly suggestions for common error shapes.
func enrichErrorPayload(e *apiErrorPayload) *apiErrorPayload {
if e == nil {
return nil
}
msg := strings.ToLower(e.Message)
code := strings.ToUpper(e.Code)
switch {
case strings.Contains(msg, "unexpected keyword argument"):
if e.Suggestion == "" {
e.Suggestion = "Check the entrypoint's expected parameter names. If your agent expects 'message', pass Kw(\"message\", ...)."
}
case strings.Contains(msg, "entrypoint") && strings.Contains(msg, "not found"):
if e.Suggestion == "" {
e.Suggestion = "Verify the entrypoint tag and use GetArchitecture(ctx) to list available tags."
}
case code == "AUTHENTICATION_ERROR":
if e.Suggestion == "" {
e.Suggestion = "Set RUNAGENT_API_KEY or pass Config.APIKey for remote calls."
}
case code == "NON_STREAM_ENTRYPOINT":
if e.Suggestion == "" {
e.Suggestion = "Use client.Run(...) for non-stream tags."
}
case code == "STREAM_ENTRYPOINT":
if e.Suggestion == "" {
e.Suggestion = "Use client.RunStream(...) for *_stream tags."
}
}
return e
}
// formatFriendlyError renders a helpful panic message including suggestions when available.
func formatFriendlyError(err error) string {
switch e := err.(type) {
case *RunAgentExecutionError:
if e.Suggestion != "" {
return fmt.Sprintf("RunAgent error: %s (%s)\nSuggestion: %s", e.Message, e.Type, e.Suggestion)
}
return fmt.Sprintf("RunAgent error: %s (%s)", e.Message, e.Type)
case *RunAgentError:
if e.Suggestion != "" {
return fmt.Sprintf("RunAgent error: %s (%s)\nSuggestion: %s", e.Message, e.Type, e.Suggestion)
}
return fmt.Sprintf("RunAgent error: %s (%s)", e.Message, e.Type)
default:
return fmt.Sprintf("RunAgent error: %v", err)
}
}