]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15064 - Improve Azure Logs
authorBelen Pruvost <belen.pruvost@sonarsource.com>
Thu, 2 Jun 2022 17:45:06 +0000 (19:45 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 3 Jun 2022 20:03:04 +0000 (20:03 +0000)
server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java

index 818169de5b0e9e9556bfb511e7baf2c978c29b91..5b318152cd3ab94a6eabb99097169c1895c5cb17 100644 (file)
@@ -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);
     }
   }
index 9d377f3fd0bd65f6d7dc4c8dd88e3ee20729506a..5b0457cff217ceb4b072456f20f4318341d91aba 100644 (file)
@@ -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