Skip to content

Extract error messages from AI service error responses#239

Merged
AnnatarHe merged 1 commit intomainfrom
claude/improve-query-error-messages-yoro8
Feb 23, 2026
Merged

Extract error messages from AI service error responses#239
AnnatarHe merged 1 commit intomainfrom
claude/improve-query-error-messages-yoro8

Conversation

@AnnatarHe
Copy link
Contributor

@AnnatarHe AnnatarHe commented Feb 23, 2026

Summary

Improved error handling in the AI service to extract and return detailed error messages from API error responses, providing better feedback to users instead of generic HTTP status codes.

Key Changes

  • Modified QueryCommandStream in ai_service.go to parse error response bodies and extract the ErrorMessage field when the API returns a non-200 status code
  • Falls back to the generic "server returned status X" message if the response body cannot be parsed or contains an empty error message
  • Added comprehensive test coverage in ai_service_test.go with test cases for:
    • Quota exceeded errors (429)
    • Unauthorized errors (401)
    • Service unavailable errors (503)
    • Non-JSON responses
    • Empty error messages

Implementation Details

  • When an error response is received, the code now attempts to read and unmarshal the response body as JSON
  • If successful and the ErrorMessage field is non-empty, that message is returned to the caller
  • The fallback behavior ensures backward compatibility and handles edge cases gracefully
  • All error scenarios are covered by unit tests to prevent regressions

https://claude.ai/code/session_01MBUEGHLc7PNCCemm4Cddnt


Open with Devin

Read and display the actual error message from the server's JSON response
instead of showing a generic "server returned status N" message. This
improves UX when hitting quota limits or other server-side errors.

https://claude.ai/code/session_01MBUEGHLc7PNCCemm4Cddnt
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@AnnatarHe AnnatarHe merged commit 7252d8a into main Feb 23, 2026
2 checks passed
@AnnatarHe AnnatarHe deleted the claude/improve-query-error-messages-yoro8 branch February 23, 2026 03:57
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @AnnatarHe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the AI service's error reporting by enabling the extraction of specific error messages from API responses. This change provides users with more actionable and descriptive error feedback, moving beyond generic HTTP status codes, and ensures the system gracefully handles various error conditions through robust testing.

Highlights

  • Enhanced Error Handling: Modified the QueryCommandStream function in ai_service.go to parse and extract detailed ErrorMessage fields from non-200 HTTP API responses, providing more specific feedback.
  • Robust Fallback Mechanism: Implemented a fallback to a generic 'server returned status X' message if the error response body cannot be parsed as JSON or if the ErrorMessage field is empty.
  • Comprehensive Test Coverage: Added extensive unit tests in ai_service_test.go to validate the new error handling logic across various scenarios, including quota exceeded (429), unauthorized (401), service unavailable (503), non-JSON responses, and empty error messages.
Changelog
  • model/ai_service.go
    • Imported the io package for reading response bodies.
    • Modified QueryCommandStream to read the resp.Body when a non-http.StatusOK status is received.
    • Added logic to unmarshal the response body into an errorResponse struct.
    • Implemented conditional return of errorResponse.ErrorMessage if successfully parsed and not empty.
    • Maintained fallback to fmt.Errorf("server returned status %d", resp.StatusCode) for unparseable or empty error bodies.
  • model/ai_service_test.go
    • Added a new test file ai_service_test.go to cover error handling scenarios.
    • Introduced TestQueryCommandStream_ErrorResponseBody with a suite of test cases.
    • Included tests for specific HTTP status codes (429, 401, 503) with custom error messages.
    • Added test cases for non-JSON responses and responses with empty error messages to verify fallback behavior.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 37.77% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
model/ai_service.go 44.18% <100.00%> (+44.18%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces improved error handling for the AI service by extracting detailed error messages from API error responses. This change enhances user feedback by providing more specific error information instead of generic HTTP status codes. The implementation correctly falls back to a generic message if the response body cannot be parsed or contains an empty error message. Comprehensive test coverage has been added to validate various error scenarios, including quota exceeded, unauthorized, service unavailable, non-JSON responses, and empty error messages. The changes are well-implemented and tested, significantly improving the robustness of the AI service's error reporting.

Comment on lines +60 to +66
body, readErr := io.ReadAll(resp.Body)
if readErr == nil {
var errResp errorResponse
if jsonErr := json.Unmarshal(body, &errResp); jsonErr == nil && errResp.ErrorMessage != "" {
return fmt.Errorf("%s", errResp.ErrorMessage)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The errorResponse struct is used here but not defined in ai_service.go. It should be defined in model/ai_service.go or imported from model/api.base.go if it's a common type. Defining it directly in ai_service.go would make this file self-contained for its error handling logic.

body, readErr := io.ReadAll(resp.Body)
if readErr == nil {
	type errorResponse struct {
		ErrorCode    int    `json:"errorCode"`
		ErrorMessage string `json:"errorMessage"`
	}
	var errResp errorResponse
	if jsonErr := json.Unmarshal(body, &errResp); jsonErr == nil && errResp.ErrorMessage != "" {
		return fmt.Errorf("%s", errResp.ErrorMessage)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants