From 921782160c1f07577035db56b5461a2edc3b30a8 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 28 Jun 2025 00:36:36 +0300 Subject: feat: improve Codeberg API error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enhanced error messages for Codeberg repository creation failures by: - Reading and parsing API error response body - Displaying the actual error message from Codeberg API - This helped identify permission scope issues (e.g., missing write:user scope) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/codeberg/codeberg.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'internal/codeberg/codeberg.go') diff --git a/internal/codeberg/codeberg.go b/internal/codeberg/codeberg.go index 1cf5659..4eaed66 100644 --- a/internal/codeberg/codeberg.go +++ b/internal/codeberg/codeberg.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "net/http" "os" "path/filepath" @@ -226,7 +227,23 @@ func (c *Client) CreateRepo(repoName, description string, private bool) error { defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { - return fmt.Errorf("failed to create repository: status code %d", resp.StatusCode) + // Read the response body to get more detailed error information + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to create repository: status code %d (could not read response)", resp.StatusCode) + } + + // Try to parse as JSON error response + var errorResp map[string]interface{} + if err := json.Unmarshal(body, &errorResp); err == nil { + // If we can parse the JSON, extract the message + if msg, ok := errorResp["message"].(string); ok { + return fmt.Errorf("failed to create repository: %s (status code %d)", msg, resp.StatusCode) + } + } + + // If we can't parse JSON, return the raw response + return fmt.Errorf("failed to create repository: %s (status code %d)", string(body), resp.StatusCode) } return nil -- cgit v1.2.3