Skip to content

Commit 8c5af0e

Browse files
committed
Add additional error handling on delete_file
1 parent 33d0402 commit 8c5af0e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pkg/github/repositories.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,14 @@ func DeleteFile(getClient GetClientFn, t translations.TranslationHelperFunc) (to
632632
}
633633
defer func() { _ = resp.Body.Close() }()
634634

635+
if resp.StatusCode != http.StatusOK {
636+
body, err := io.ReadAll(resp.Body)
637+
if err != nil {
638+
return nil, fmt.Errorf("failed to read response body: %w", err)
639+
}
640+
return mcp.NewToolResultError(fmt.Sprintf("failed to get commit: %s", string(body))), nil
641+
}
642+
635643
// Create a tree entry for the file deletion by setting SHA to nil
636644
treeEntries := []*github.TreeEntry{
637645
{
@@ -649,6 +657,14 @@ func DeleteFile(getClient GetClientFn, t translations.TranslationHelperFunc) (to
649657
}
650658
defer func() { _ = resp.Body.Close() }()
651659

660+
if resp.StatusCode != http.StatusCreated {
661+
body, err := io.ReadAll(resp.Body)
662+
if err != nil {
663+
return nil, fmt.Errorf("failed to read response body: %w", err)
664+
}
665+
return mcp.NewToolResultError(fmt.Sprintf("failed to create tree: %s", string(body))), nil
666+
}
667+
652668
// Create a new commit with the new tree
653669
commit := &github.Commit{
654670
Message: github.Ptr(message),
@@ -661,6 +677,14 @@ func DeleteFile(getClient GetClientFn, t translations.TranslationHelperFunc) (to
661677
}
662678
defer func() { _ = resp.Body.Close() }()
663679

680+
if resp.StatusCode != http.StatusCreated {
681+
body, err := io.ReadAll(resp.Body)
682+
if err != nil {
683+
return nil, fmt.Errorf("failed to read response body: %w", err)
684+
}
685+
return mcp.NewToolResultError(fmt.Sprintf("failed to create commit: %s", string(body))), nil
686+
}
687+
664688
// Update the branch reference to point to the new commit
665689
ref.Object.SHA = newCommit.SHA
666690
_, resp, err = client.Git.UpdateRef(ctx, owner, repo, ref, false)
@@ -669,6 +693,14 @@ func DeleteFile(getClient GetClientFn, t translations.TranslationHelperFunc) (to
669693
}
670694
defer func() { _ = resp.Body.Close() }()
671695

696+
if resp.StatusCode != http.StatusOK {
697+
body, err := io.ReadAll(resp.Body)
698+
if err != nil {
699+
return nil, fmt.Errorf("failed to read response body: %w", err)
700+
}
701+
return mcp.NewToolResultError(fmt.Sprintf("failed to update reference: %s", string(body))), nil
702+
}
703+
672704
// Create a response similar to what the DeleteFile API would return
673705
response := map[string]interface{}{
674706
"commit": newCommit,

0 commit comments

Comments
 (0)