aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBelen Pruvost <belen.pruvost@sonarsource.com>2022-06-02 19:45:06 +0200
committersonartech <sonartech@sonarsource.com>2022-06-03 20:03:04 +0000
commitd06cd9eb086e29f18e504a5adf022849ff23f6a3 (patch)
tree88a0e21e98b5f2fc9e8dba15fe88b185c391edbe
parent83ae2064eca6777fe0247625d0ce8f4346a99d0f (diff)
downloadsonarqube-d06cd9eb086e29f18e504a5adf022849ff23f6a3.tar.gz
sonarqube-d06cd9eb086e29f18e504a5adf022849ff23f6a3.zip
SONAR-15064 - Improve Azure Logs
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java17
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java27
2 files changed, 41 insertions, 3 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
index 818169de5b0..5b318152cd3 100644
--- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
@@ -108,6 +108,7 @@ public class AzureDevOpsHttpClient {
try (Response response = client.newCall(request).execute()) {
checkResponseIsSuccessful(response);
} catch (IOException e) {
+ LOG.error(String.format(UNABLE_TO_CONTACT_AZURE_SERVER + " for request [%s]: [%s]", request.url(), e.getMessage()));
throw new IllegalArgumentException(UNABLE_TO_CONTACT_AZURE_SERVER, e);
}
}
@@ -122,8 +123,12 @@ public class AzureDevOpsHttpClient {
checkResponseIsSuccessful(response);
return handler.apply(response);
} catch (JsonSyntaxException e) {
+ LOG.error(String.format("Response from Azure for request [%s] could not be parsed: [%s]",
+ request.url(),
+ e.getMessage()));
throw new IllegalArgumentException(UNABLE_TO_CONTACT_AZURE_SERVER + ", got an unexpected response", e);
} catch (IOException e) {
+ LOG.error(String.format(UNABLE_TO_CONTACT_AZURE_SERVER + " for request [%s]: [%s]", request.url(), e.getMessage()));
throw new IllegalArgumentException(UNABLE_TO_CONTACT_AZURE_SERVER, e);
}
}
@@ -138,14 +143,22 @@ public class AzureDevOpsHttpClient {
protected static void checkResponseIsSuccessful(Response response) throws IOException {
if (!response.isSuccessful()) {
- LOG.debug(UNABLE_TO_CONTACT_AZURE_SERVER + ": {} {}", response.request().url().toString(), response.code());
if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
+ LOG.error(String.format(UNABLE_TO_CONTACT_AZURE_SERVER + " for request [%s]: Invalid personal access token",
+ response.request().url()));
throw new AzureDevopsServerException(response.code(), "Invalid personal access token");
}
+
+ if (response.code() == HttpURLConnection.HTTP_NOT_FOUND) {
+ LOG.error(String.format(UNABLE_TO_CONTACT_AZURE_SERVER + " for request [%s]: URL Not Found",
+ response.request().url()));
+ throw new AzureDevopsServerException(response.code(), "Invalid Azure URL");
+ }
+
ResponseBody responseBody = response.body();
String body = responseBody == null ? "" : responseBody.string();
String errorMessage = generateErrorMessage(body, UNABLE_TO_CONTACT_AZURE_SERVER);
- LOG.info(String.format("Azure API call to [%s] failed with %s http code. Azure response content : [%s]", response.request().url().toString(), response.code(), body));
+ LOG.error(String.format("Azure API call to [%s] failed with %s http code. Azure response content : [%s]", response.request().url(), response.code(), body));
throw new AzureDevopsServerException(response.code(), errorMessage);
}
}
diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java
index 9d377f3fd0b..5b0457cff21 100644
--- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java
+++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java
@@ -137,7 +137,7 @@ public class AzureDevOpsHttpClientTest {
assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
assertThat(logTester.logs(LoggerLevel.DEBUG))
- .contains("get projects : [" + server.url("").toString() + "_apis/projects?api-version=3.0]");
+ .contains("get projects : [" + server.url("") + "_apis/projects?api-version=3.0]");
assertThat(projects.getValues()).hasSize(2);
assertThat(projects.getValues())
.extracting(GsonAzureProject::getName, GsonAzureProject::getDescription)
@@ -151,6 +151,10 @@ public class AzureDevOpsHttpClientTest {
assertThatThrownBy(() -> underTest.getProjects(server.url("").toString(), "token"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(UNABLE_TO_CONTACT_AZURE);
+
+ assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1);
+ assertThat(logTester.logs(LoggerLevel.ERROR).iterator().next())
+ .contains("Response from Azure for request [" + server.url("") + "_apis/projects?api-version=3.0] could not be parsed:");
}
@Test
@@ -160,6 +164,23 @@ public class AzureDevOpsHttpClientTest {
assertThatThrownBy(() -> underTest.getProjects(server.url("").toString(), "invalid-token"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid personal access token");
+
+ assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1);
+ assertThat(logTester.logs(LoggerLevel.ERROR).iterator().next())
+ .contains("Unable to contact Azure DevOps server for request [" + server.url("") + "_apis/projects?api-version=3.0]: Invalid personal access token");
+ }
+
+ @Test
+ public void get_projects_with_invalid_url() {
+ enqueueResponse(404);
+
+ assertThatThrownBy(() -> underTest.getProjects(server.url("").toString(), "invalid-token"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Invalid Azure URL");
+
+ assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1);
+ assertThat(logTester.logs(LoggerLevel.ERROR).iterator().next())
+ .contains("Unable to contact Azure DevOps server for request [" + server.url("") + "_apis/projects?api-version=3.0]: URL Not Found");
}
@Test
@@ -169,6 +190,10 @@ public class AzureDevOpsHttpClientTest {
assertThatThrownBy(() -> underTest.getProjects(server.url("").toString(), "token"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unable to contact Azure DevOps server");
+
+ assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1);
+ assertThat(logTester.logs(LoggerLevel.ERROR).iterator().next())
+ .contains("Azure API call to [" + server.url("") + "_apis/projects?api-version=3.0] failed with 500 http code. Azure response content :");
}
@Test