feat: add PR diff, PR review, and structured error handling commands

This commit is contained in:
sid 2026-03-21 21:50:24 -06:00
parent 3db03ed5e2
commit 50191cc542
10 changed files with 1008 additions and 13 deletions

View file

@ -76,7 +76,11 @@ func (c *Client) GetJSON(path string, result any) error {
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
return &APIError{
StatusCode: resp.StatusCode,
Body: string(body),
Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(body)),
}
}
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
@ -134,7 +138,11 @@ func (c *Client) DoJSON(method string, path string, body any, result any) (int,
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
bodyBytes, _ := io.ReadAll(resp.Body)
return resp.StatusCode, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(bodyBytes))
return resp.StatusCode, &APIError{
StatusCode: resp.StatusCode,
Body: string(bodyBytes),
Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(bodyBytes)),
}
}
if result != nil && resp.StatusCode != http.StatusNoContent {
@ -171,7 +179,11 @@ func (c *Client) GetRawLog(url string) (string, error) {
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
return "", &APIError{
StatusCode: resp.StatusCode,
Body: string(body),
Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(body)),
}
}
bodyBytes, err := io.ReadAll(resp.Body)

17
internal/api/errors.go Normal file
View file

@ -0,0 +1,17 @@
package api
import "fmt"
// APIError represents a non-2xx HTTP response from the Forgejo API.
type APIError struct {
StatusCode int
Body string
Message string
}
func (e *APIError) Error() string {
if e.Body != "" {
return fmt.Sprintf("API request failed with status %d: %s", e.StatusCode, e.Body)
}
return fmt.Sprintf("API request failed with status %d: %s", e.StatusCode, e.Message)
}