Skip to content

Commit a556214

Browse files
committed
Handle non-404 errors
1 parent 35a514c commit a556214

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

pkg/github/repositories.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,17 @@ SHA MUST be provided for existing file updates.
428428
if respCheck != nil {
429429
_ = respCheck.Body.Close()
430430
}
431-
if getErr == nil && existingFile != nil {
431+
if getErr != nil {
432+
// 404 means file doesn't exist - proceed (new file creation)
433+
// Any other error (403, 500, network) should be surfaced
434+
if respCheck == nil || respCheck.StatusCode != http.StatusNotFound {
435+
return ghErrors.NewGitHubAPIErrorResponse(ctx,
436+
"failed to verify file SHA",
437+
respCheck,
438+
getErr,
439+
), nil, nil
440+
}
441+
} else if existingFile != nil {
432442
currentSHA := existingFile.GetSHA()
433443
if currentSHA != sha {
434444
return utils.NewToolResultError(fmt.Sprintf(
@@ -437,21 +447,30 @@ SHA MUST be provided for existing file updates.
437447
sha, currentSHA, path)), nil, nil
438448
}
439449
}
440-
// If file not found or error, proceed (could be a new file creation)
441450
} else {
442451
// No SHA provided - check if file already exists
443452
existingFile, _, respCheck, getErr := client.Repositories.GetContents(ctx, owner, repo, path, getOpts)
444453
if respCheck != nil {
445454
_ = respCheck.Body.Close()
446455
}
447-
if getErr == nil && existingFile != nil {
456+
if getErr != nil {
457+
// 404 means file doesn't exist - proceed with creation
458+
// Any other error (403, 500, network) should be surfaced
459+
if respCheck == nil || respCheck.StatusCode != http.StatusNotFound {
460+
return ghErrors.NewGitHubAPIErrorResponse(ctx,
461+
"failed to check if file exists",
462+
respCheck,
463+
getErr,
464+
), nil, nil
465+
}
466+
} else if existingFile != nil {
448467
// File exists but no SHA was provided - reject to prevent blind overwrites
449468
return utils.NewToolResultError(fmt.Sprintf(
450469
"File already exists at %s. You must provide the current file's SHA when updating. "+
451470
"Use git rev-parse HEAD:%s to get the blob SHA, then retry with the sha parameter.",
452471
path, path)), nil, nil
453472
}
454-
// If file not found or error, no previous SHA needed (new file creation)
473+
// If file not found, no previous SHA needed (new file creation)
455474
}
456475

457476
fileContent, resp, err := client.Repositories.CreateFile(ctx, owner, repo, path, opts)

0 commit comments

Comments
 (0)