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);
}
}
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);
}
}
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);
}
}
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)
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
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
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