-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
126 lines (103 loc) · 3.74 KB
/
errors.go
File metadata and controls
126 lines (103 loc) · 3.74 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
package agentpg
import (
"errors"
"fmt"
)
// Sentinel errors for AgentPG operations.
var (
// Configuration errors
ErrInvalidConfig = errors.New("invalid configuration")
// Resource not found errors
ErrSessionNotFound = errors.New("session not found")
ErrRunNotFound = errors.New("run not found")
ErrAgentNotFound = errors.New("agent not found")
ErrToolNotFound = errors.New("tool not found")
ErrIterationNotFound = errors.New("iteration not found")
ErrToolExecutionNotFound = errors.New("tool execution not found")
// Registration errors
ErrAgentNotRegistered = errors.New("agent not registered on this client")
ErrToolNotRegistered = errors.New("tool not registered on this client")
// Client lifecycle errors
ErrClientNotStarted = errors.New("client not started")
ErrClientAlreadyStarted = errors.New("client already started")
ErrClientStopping = errors.New("client is stopping")
// State errors
ErrInvalidStateTransition = errors.New("invalid state transition")
ErrRunAlreadyFinalized = errors.New("run already finalized")
ErrRunCancelled = errors.New("run was cancelled")
ErrRunNotCancellable = errors.New("run is not in a cancellable state")
// Tool errors
ErrInvalidToolSchema = errors.New("invalid tool schema")
ErrToolExecutionFailed = errors.New("tool execution failed")
// Batch API errors
ErrBatchAPIError = errors.New("claude batch API error")
ErrBatchExpired = errors.New("batch expired")
ErrBatchFailed = errors.New("batch processing failed")
// Storage errors
ErrStorageError = errors.New("storage operation failed")
// Instance errors
ErrInstanceDisconnected = errors.New("instance disconnected")
ErrInstanceNotFound = errors.New("instance not found")
// Compaction errors
ErrCompactionFailed = errors.New("context compaction failed")
)
// AgentError provides structured error context for AgentPG operations.
// It wraps an underlying error with additional context including operation name,
// session/run IDs, and arbitrary key-value context.
type AgentError struct {
// Op is the operation that failed (e.g., "Run", "NewSession", "ExecuteTool")
Op string
// Err is the underlying error
Err error
// SessionID is the session ID if applicable
SessionID string
// RunID is the run ID if applicable
RunID string
// Context holds additional key-value pairs for debugging
Context map[string]any
}
// Error returns a formatted error message.
func (e *AgentError) Error() string {
msg := e.Op + ": " + e.Err.Error()
if e.SessionID != "" {
msg += fmt.Sprintf(" (session=%s)", e.SessionID)
}
if e.RunID != "" {
msg += fmt.Sprintf(" (run=%s)", e.RunID)
}
return msg
}
// Unwrap returns the underlying error for errors.Is/errors.As support.
func (e *AgentError) Unwrap() error {
return e.Err
}
// NewAgentError creates a new AgentError with the given operation and underlying error.
func NewAgentError(op string, err error) *AgentError {
return &AgentError{
Op: op,
Err: err,
Context: make(map[string]any),
}
}
// WithSession sets the session ID on the error and returns the error for chaining.
func (e *AgentError) WithSession(sessionID string) *AgentError {
e.SessionID = sessionID
return e
}
// WithRun sets the run ID on the error and returns the error for chaining.
func (e *AgentError) WithRun(runID string) *AgentError {
e.RunID = runID
return e
}
// WithContext adds a key-value pair to the error context and returns the error for chaining.
func (e *AgentError) WithContext(key string, value any) *AgentError {
e.Context[key] = value
return e
}
// WrapError wraps an error with operation context. If err is nil, returns nil.
func WrapError(op string, err error) error {
if err == nil {
return nil
}
return NewAgentError(op, err)
}