Skip to content

Conversation

@GauravJangra9988
Copy link
Contributor

@GauravJangra9988 GauravJangra9988 commented Oct 4, 2025

Description

Handle the status other then OK , for claude API request.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [x ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Other (please describe):

Related Issue

#20

Fixes #(issue number)
#20

Changes Made

  • Added validation for unsuccessful API request
  • Return error with relevant http status code

Testing

  • Tested with Gemini API
  • Tested with Grok API
  • [x ] Tested on Windows
  • Tested on Linux
  • Tested on macOS
  • Added/updated tests (if applicable)

Checklist

  • My code follows the project's code style
  • [x ] I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • [x ] My changes generate no new warnings or errors
  • [x ] I have tested this in a real Git repository
  • [x ] I have read the CONTRIBUTING.md guidelines

Screenshots (if applicable)

Additional Notes


For Hacktoberfest Participants

  • [x ] This PR is submitted as part of Hacktoberfest 2025

Thank you for your contribution! 🎉

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of AI service responses to prevent processing invalid results.
    • Users now receive clearer errors when the AI service is unavailable or returns a non-OK status.
    • Reduces confusing failures and avoids partial or misleading outputs by stopping early on bad responses.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 4, 2025

Walkthrough

Added an HTTP status code check in src/claude/claude.go to return an error for non-200 responses before decoding the body, altering control flow and error handling.

Changes

Cohort / File(s) Summary
HTTP error handling in Claude client
src/claude/claude.go
Inserted a post-request status check; on non-200, returns error "claude AI response " and skips response decoding.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant C as Caller
  participant CL as ClaudeClient
  participant H as HTTP API

  C->>CL: RequestCompletion(input)
  CL->>H: POST /complete
  H-->>CL: HTTP Response (status, body)

  alt Status != 200
    note right of CL: New behavior
    CL-->>C: error("claude AI response <code>")
  else Status == 200
    CL->>CL: Decode body
    CL-->>C: Completion result
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A bunny thumps the ground—ok checks in place,
If status isn’t sunny, we bail from the race.
No nibbling bad bytes, no guessing the tale,
Hop back with an error, quick as a tail.
When 200 arrives—we feast on the kale. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed This title clearly and concisely summarizes the pull request’s primary change by indicating that non-200 status codes from the Claude API will now be handled, making it immediately clear to reviewers why the update was made.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/claude/claude.go (1)

70-72: Consider adding tests for error handling.

Since this PR introduces new error handling logic, it would be beneficial to add automated tests covering:

  • Non-200 status codes (e.g., 400, 401, 429, 500)
  • Verification that error messages include status codes and response details
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 06457ae and e3e543a.

📒 Files selected for processing (1)
  • src/claude/claude.go (1 hunks)
🔇 Additional comments (1)
src/claude/claude.go (1)

70-72: Other AI provider implementations handle errors correctly
Grok already reads and includes the response body on non-200 status, and Gemini uses the genai client (which surfaces HTTP errors internally), so no changes are needed for those providers.

Comment on lines +70 to +72
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("claude AI response %d", resp.StatusCode)
}
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.

@DFanso DFanso self-requested a review October 4, 2025 13:55
@DFanso DFanso added bug Something isn't working hacktoberfest Eligible for Hacktoberfest labels Oct 4, 2025
Copy link
Owner

@DFanso DFanso left a comment

Choose a reason for hiding this comment

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

hey @GauravJangra9988 can u check coderabbitai review also

@DFanso DFanso added good first issue Good for newcomers go Pull requests that update go code labels Oct 4, 2025
@GauravJangra9988
Copy link
Contributor Author

Checked the Code Rabbit review it is OK.

@DFanso DFanso self-requested a review October 4, 2025 14:06
Copy link
Owner

@DFanso DFanso left a comment

Choose a reason for hiding this comment

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

LGTM 🎊

@DFanso DFanso merged commit 873a1fe into DFanso:main Oct 4, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code good first issue Good for newcomers hacktoberfest Eligible for Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants