Skip to content

Commit 63649f2

Browse files
committed
fix(github): improve error reporting for GitHub file fetches
I am improving the error handling when fetching files from GitHub. Previously, if reading the response body failed, the error message would be incomplete. Now, I ensure that even if reading the response body fails, the error message will indicate this, providing more context for debugging. This change also renames a variable to clarify its purpose in the `GeminiAskHandler`.
1 parent eb0c39e commit 63649f2

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

direct_handlers.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func (s *GeminiServer) GeminiAskHandler(ctx context.Context, req mcp.CallToolReq
6666
}
6767
}
6868

69-
uploads, fileErrs := fetchFromGitHub(ctx, s, githubRepo, githubRef, githubFiles)
69+
fetchedUploads, fileErrs := fetchFromGitHub(ctx, s, githubRepo, githubRef, githubFiles)
70+
uploads = fetchedUploads
7071
if len(fileErrs) > 0 {
7172
for _, err := range fileErrs {
7273
logger.Error("Error processing github file: %v", err)
@@ -665,8 +666,14 @@ func fetchFromGitHub(ctx context.Context, s *GeminiServer, repoURL, ref string,
665666
defer resp.Body.Close()
666667

667668
if resp.StatusCode != http.StatusOK {
668-
body, _ := io.ReadAll(resp.Body)
669-
errChannel <- fmt.Errorf("failed to fetch %s: status %d, body: %s", filePath, resp.StatusCode, string(body))
669+
var bodyMsg string
670+
body, err := io.ReadAll(resp.Body)
671+
if err != nil {
672+
bodyMsg = fmt.Sprintf("(could not read response body: %v)", err)
673+
} else {
674+
bodyMsg = string(body)
675+
}
676+
errChannel <- fmt.Errorf("failed to fetch %s: status %d, body: %s", filePath, resp.StatusCode, bodyMsg)
670677
return
671678
}
672679
logger.Info("Successfully connected to GitHub and fetched: %s", filePath)

logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,6 @@ func (l *StandardLogger) Error(format string, args ...interface{}) {
8383
func (l *StandardLogger) log(level, format string, args ...interface{}) {
8484
timestamp := time.Now().Format("2006-01-02 15:04:05")
8585
message := fmt.Sprintf(format, args...)
86+
//nolint:errcheck
8687
fmt.Fprintf(l.writer, "[%s] %s: %s\n", timestamp, level, message)
8788
}

0 commit comments

Comments
 (0)