From d06cd9eb086e29f18e504a5adf022849ff23f6a3 Mon Sep 17 00:00:00 2001 From: Belen Pruvost Date: Thu, 2 Jun 2022 19:45:06 +0200 Subject: [PATCH] SONAR-15064 - Improve Azure Logs --- .../client/azure/AzureDevOpsHttpClient.java | 17 ++++++++++-- .../azure/AzureDevOpsHttpClientTest.java | 27 ++++++++++++++++++- 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 -- 2.39.5