Export GetError and SetError methods (#753)
Exported GetError and SetError. Fixes #64 Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
parent
b4f957ff3c
commit
13488f7da1
3 changed files with 8 additions and 8 deletions
|
|
@ -2124,7 +2124,7 @@ func TestToolErrorMiddleware(t *testing.T) {
|
||||||
res, err := h(ctx, method, req)
|
res, err := h(ctx, method, req)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if ctr, ok := res.(*CallToolResult); ok {
|
if ctr, ok := res.(*CallToolResult); ok {
|
||||||
middleErr = ctr.getError()
|
middleErr = ctr.GetError()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res, err
|
return res, err
|
||||||
|
|
@ -2161,7 +2161,7 @@ func TestToolErrorMiddleware(t *testing.T) {
|
||||||
t.Fatal("want error, got none")
|
t.Fatal("want error, got none")
|
||||||
}
|
}
|
||||||
// Clients can't see the error, because it isn't marshaled.
|
// Clients can't see the error, because it isn't marshaled.
|
||||||
if err := res.getError(); err != nil {
|
if err := res.GetError(); err != nil {
|
||||||
t.Fatalf("got %v, want nil", err)
|
t.Fatalf("got %v, want nil", err)
|
||||||
}
|
}
|
||||||
if middleErr != errTestFailure {
|
if middleErr != errTestFailure {
|
||||||
|
|
|
||||||
|
|
@ -116,17 +116,17 @@ type CallToolResult struct {
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(#64): consider exposing setError (and getError), by adding an error
|
// SetError sets the error for the tool result and populates the Content field
|
||||||
// field on CallToolResult.
|
// with the error text. It also sets IsError to true.
|
||||||
func (r *CallToolResult) setError(err error) {
|
func (r *CallToolResult) SetError(err error) {
|
||||||
r.Content = []Content{&TextContent{Text: err.Error()}}
|
r.Content = []Content{&TextContent{Text: err.Error()}}
|
||||||
r.IsError = true
|
r.IsError = true
|
||||||
r.err = err
|
r.err = err
|
||||||
}
|
}
|
||||||
|
|
||||||
// getError returns the error set with setError, or nil if none.
|
// GetError returns the error set with SetError, or nil if none.
|
||||||
// This function always returns nil on clients.
|
// This function always returns nil on clients.
|
||||||
func (r *CallToolResult) getError() error {
|
func (r *CallToolResult) GetError() error {
|
||||||
return r.err
|
return r.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -339,7 +339,7 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
|
||||||
}
|
}
|
||||||
// For regular errors, embed them in the tool result as per MCP spec
|
// For regular errors, embed them in the tool result as per MCP spec
|
||||||
var errRes CallToolResult
|
var errRes CallToolResult
|
||||||
errRes.setError(err)
|
errRes.SetError(err)
|
||||||
return &errRes, nil
|
return &errRes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue