Skip to content

Commit 32cb4db

Browse files
feat(server): add Tool call error categories (#2387)
Create Agent vs Server error types to distinguish between the two types. --------- Co-authored-by: Averi Kitsch <akitsch@google.com>
1 parent 1fdd99a commit 32cb4db

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

internal/util/errors.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2026 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
package util
14+
15+
import "fmt"
16+
17+
type ErrorCategory string
18+
19+
const (
20+
CategoryAgent ErrorCategory = "AGENT_ERROR"
21+
CategoryServer ErrorCategory = "SERVER_ERROR"
22+
)
23+
24+
// ToolboxError is the interface all custom errors must satisfy
25+
type ToolboxError interface {
26+
error
27+
Category() ErrorCategory
28+
Error() string
29+
Unwrap() error
30+
}
31+
32+
// Agent Errors return 200 to the sender
33+
type AgentError struct {
34+
Msg string
35+
Cause error
36+
}
37+
38+
var _ ToolboxError = &AgentError{}
39+
40+
func (e *AgentError) Error() string {
41+
if e.Cause != nil {
42+
return fmt.Sprintf("%s: %v", e.Msg, e.Cause)
43+
}
44+
return e.Msg
45+
}
46+
47+
func (e *AgentError) Category() ErrorCategory { return CategoryAgent }
48+
49+
func (e *AgentError) Unwrap() error { return e.Cause }
50+
51+
func NewAgentError(msg string, cause error) *AgentError {
52+
return &AgentError{Msg: msg, Cause: cause}
53+
}
54+
55+
// ClientServerError returns 4XX/5XX error code
56+
type ClientServerError struct {
57+
Msg string
58+
Code int
59+
Cause error
60+
}
61+
62+
var _ ToolboxError = &ClientServerError{}
63+
64+
func (e *ClientServerError) Error() string {
65+
if e.Cause != nil {
66+
return fmt.Sprintf("%s: %v", e.Msg, e.Cause)
67+
}
68+
return e.Msg
69+
}
70+
71+
func (e *ClientServerError) Category() ErrorCategory { return CategoryServer }
72+
73+
func (e *ClientServerError) Unwrap() error { return e.Cause }
74+
75+
func NewClientServerError(msg string, code int, cause error) *ClientServerError {
76+
return &ClientServerError{Msg: msg, Code: code, Cause: cause}
77+
}

0 commit comments

Comments
 (0)