Skip to content

Conversation

@eray-felek-sonarsource
Copy link
Contributor

No description provided.

@hashicorp-vault-sonar-prod
Copy link

hashicorp-vault-sonar-prod bot commented Feb 11, 2026

MCP-274

@eray-felek-sonarsource eray-felek-sonarsource force-pushed the feature/ef/MCP-274-handle-http-responses-better branch from 277df71 to 02d962e Compare February 11, 2026 14:50
@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 11, 2026

@eray-felek-sonarsource eray-felek-sonarsource marked this pull request as ready for review February 11, 2026 15:00
@nquinquenel nquinquenel requested a review from Copilot February 12, 2026 09:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improve error handling for HTTP responses by extracting useful messages from JSON error bodies and making response body parsing more resilient.

Changes:

  • Extend JSON error parsing to support a top-level message field when errors is absent.
  • Add a fallback in HttpResponse.bodyAsString() to decode body bytes when text is unavailable.
  • Add tests covering these new behaviors.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/main/java/org/sonarsource/sonarqube/mcp/serverapi/ServerApiHelper.java Parse message from JSON error bodies when errors is missing
src/main/java/org/sonarsource/sonarqube/mcp/http/HttpResponse.java Fallback from getBodyText() to UTF-8 decoded getBodyBytes()
src/test/java/org/sonarsource/sonarqube/mcp/serverapi/ServerApiTests.java Tests for message parsing and ignoring irrelevant JSON bodies
src/test/java/org/sonarsource/sonarqube/mcp/http/HttpClientProviderTests.java Test for bodyAsString() fallback when getBodyText() is null

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +192 to +194
var message = obj.getAsJsonPrimitive("message");
if (message != null) {
return message.getAsString();
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

JsonObject#getAsJsonPrimitive("message") can throw if message exists but is not a JSON primitive (e.g., null, object, array). To make this robust, fetch via obj.get("message") and only read it when the element is a non-null primitive (e.g., element != null && element.isJsonPrimitive() && !element.isJsonNull()).

Suggested change
var message = obj.getAsJsonPrimitive("message");
if (message != null) {
return message.getAsString();
JsonElement messageElement = obj.get("message");
if (messageElement != null && messageElement.isJsonPrimitive() && !messageElement.isJsonNull()) {
return messageElement.getAsJsonPrimitive().getAsString();

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

Seems like a valid consideration

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