Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/claude/claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func GenerateCommitMessage(config *types.Config, changes string, apiKey string)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("claude AI response %d", resp.StatusCode)
}
Comment on lines +70 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Read and include the error response body for better diagnostics.

The error message lacks details from the Claude API's response body. When the API returns a non-200 status, it typically includes a JSON error response with valuable information (e.g., rate limit details, authentication failures, invalid request specifics). Discarding this information makes debugging significantly harder for users.

Additionally, the error message "claude AI response %d" could be clearer.

Apply this diff to read the response body and include error details:

 	if resp.StatusCode != http.StatusOK {
-		return "", fmt.Errorf("claude AI response %d", resp.StatusCode) 
+		body, _ := io.ReadAll(resp.Body)
+		return "", fmt.Errorf("claude API request failed with status code %d: %s", resp.StatusCode, string(body))
 	}

Don't forget to add the io import:

 import (
 	"bytes"
 	"context"
 	"encoding/json"
 	"fmt"
+	"io"
 	"net/http"
 
 	"github.com/dfanso/commit-msg/src/types"
 )
🤖 Prompt for AI Agents
In src/claude/claude.go around lines 70 to 72, the code only returns a generic
error for non-200 responses; read the response body (remember to add the io
import) and include its contents in the returned error for better diagnostics.
Specifically, defer resp.Body.Close(), use io.ReadAll(resp.Body) (handle that
read error), trim or convert the body to a string, and return a clearer error
such as "claude AI error: status %d: %s" including both resp.StatusCode and the
response body content.


var claudeResponse ClaudeResponse
if err := json.NewDecoder(resp.Body).Decode(&claudeResponse); err != nil {
return "", err
Expand Down
Loading