SonarSource/sonarqube · error · IllegalArgumentException
Unable to contact Bitbucket server, got an unexpected respon
Error message
Unable to contact Bitbucket server, got an unexpected response
What it means
applyHandler runs the caller-provided Gson deserialization handler on the response body. If the handler throws JsonSyntaxException, the client concludes the body, while reachable, is not the expected Bitbucket JSON shape, logs the body, and throws IllegalArgumentException('Unable to contact Bitbucket server, got an unexpected response').
Source
Thrown at server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java:168
private String getBodyString(Request request) {
try (Response response = client.newCall(request).execute()) {
String bodyString = response.body().string();
validateResponseBody(response.isSuccessful(), bodyString);
handleHttpErrorIfAny(response.isSuccessful(), response.code(), bodyString);
return bodyString;
} catch (IOException e) {
LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ": {}", e.getMessage(), e);
throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER, e);
}
}
protected static <G> G applyHandler(Function<String, G> handler, String bodyString) {
try {
return handler.apply(bodyString);
} catch (JsonSyntaxException e) {
LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ". Unexpected body response was : [{}]", bodyString);
LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ": {}", e.getMessage(), e);
throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ", got an unexpected response", e);
}
}
protected static void validateResponseBody(boolean isSuccessful, String bodyString) {
if (isSuccessful) {
try {
buildGson().fromJson(bodyString, Object.class);
} catch (JsonParseException e) {
LOG.info(UNEXPECTED_RESPONSE_FROM_BITBUCKET_SERVER + " : [{}]", bodyString);
throw new IllegalArgumentException(UNEXPECTED_RESPONSE_FROM_BITBUCKET_SERVER, e);
}
}
}
protected static void handleHttpErrorIfAny(boolean isSuccessful, int httpCode, String bodyString) {
if (!isSuccessful) {
String errorMessage = getErrorMessage(bodyString);
LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ": {} {}", httpCode, errorMessage);View on GitHub (pinned to 184c821202)
Solutions
- Open the server log and inspect 'Unexpected body response was : [...]' to see the actual payload.
- If the body is HTML/login page, the URL is hitting SSO or a wrong service — fix the serverUrl or exempt SonarQube from SSO redirects.
- If the body is valid but differently-shaped JSON, check the Bitbucket Server version's REST API and upgrade the SonarQube ALM plugin/version.
- Confirm the configured URL points at the Bitbucket Server base URL, not a proxy path that rewrites responses.
Defensive patterns
Strategy: fallback
Try / catch
try {
// call client
} catch (IllegalArgumentException e) {
if (e.getMessage() != null && e.getMessage().endsWith("got an unexpected response")) {
// body was non-JSON/unexpected: inspect logged body, fall back or surface a config warning
}
} Prevention
- Check the server log for 'Unexpected body response was' to identify SSO/proxy interference.
- Exempt SonarQube's source IPs/agents from SSO redirects on Bitbucket.
- Keep the SonarQube Bitbucket integration and Bitbucket Server versions compatible.
- Point the ALM setting at the Bitbucket base URL, not a proxy path that rewrites responses.
When it happens
Trigger: A successful HTTP response whose body fails strict Gson parsing of the target model, e.g. a Bitbucket proxy/login HTML page, a CDN/WAF error page, or a Bitbucket Data Center version returning a JSON shape the handler's target class (e.g. BranchesList) cannot parse.
Common situations: Requests intercepted by an SSO/login redirect returning HTML with status 200; a reverse proxy or antivirus appliance injecting content; Bitbucket Server version mismatch changing the REST API payload; hitting a non-Bitbucket service on the configured URL.
Related errors
- url must start with http:// or https://
- Unable to contact Bitbucket server
- Unexpected response from Bitbucket server
- Invalid personal access token
- %s is not a valid url
AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09).
Data as JSON: /api/errors/b84767c1abbf17d7.
Report an issue: GitHub.