mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 09:44:21 +01:00
handle errors correctly in the other tools
This commit is contained in:
@@ -18,6 +18,11 @@ type SourcegraphParams struct {
|
||||
Timeout int `json:"timeout,omitempty"`
|
||||
}
|
||||
|
||||
type SourcegraphMetadata struct {
|
||||
NumberOfMatches int `json:"number_of_matches"`
|
||||
Truncated bool `json:"truncated"`
|
||||
}
|
||||
|
||||
type sourcegraphTool struct {
|
||||
client *http.Client
|
||||
}
|
||||
@@ -198,7 +203,7 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
|
||||
|
||||
graphqlQueryBytes, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return NewTextErrorResponse("Failed to create GraphQL request: " + err.Error()), nil
|
||||
return ToolResponse{}, fmt.Errorf("failed to marshal GraphQL request: %w", err)
|
||||
}
|
||||
graphqlQuery := string(graphqlQueryBytes)
|
||||
|
||||
@@ -209,7 +214,7 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
|
||||
bytes.NewBuffer([]byte(graphqlQuery)),
|
||||
)
|
||||
if err != nil {
|
||||
return NewTextErrorResponse("Failed to create request: " + err.Error()), nil
|
||||
return ToolResponse{}, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -217,7 +222,7 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return NewTextErrorResponse("Failed to execute request: " + err.Error()), nil
|
||||
return ToolResponse{}, fmt.Errorf("failed to fetch URL: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
@@ -231,12 +236,12 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return NewTextErrorResponse("Failed to read response body: " + err.Error()), nil
|
||||
return ToolResponse{}, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return NewTextErrorResponse("Failed to parse response: " + err.Error()), nil
|
||||
return ToolResponse{}, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
formattedResults, err := formatSourcegraphResults(result, params.ContextWindow)
|
||||
|
||||
Reference in New Issue
Block a user