Skip to content

Commit

Permalink
#64 - Add error handling in monitoring service
Browse files Browse the repository at this point in the history
- add country TR
- add try-catch-block in monitorBeaconResource function
- add ERROR in HttpStatusCategory
  • Loading branch information
vabishaa committed Oct 10, 2024
1 parent 3cc6736 commit 3c150c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,21 @@ void monitorBeaconResource(Resource resource) {
long startTime = System.currentTimeMillis();
Monitor monitor = new Monitor();
monitor.setResourceId(resource.getId());
WebClient client = WebClient.create();
Mono<String> response = client.post()
.uri( resource.getResourceAddress())
.bodyValue(defaultRequestBody)
.accept(MediaType.APPLICATION_JSON)
.header("auth-key", authKey)
.exchangeToMono(rs -> {
monitor.setResponseStatusCode(rs.statusCode().value());
return rs.bodyToMono(String.class);
});
monitor.setResponseBody(response.block());
try{
WebClient client = WebClient.create();
Mono<String> response = client.post()
.uri( resource.getResourceAddress())
.bodyValue(defaultRequestBody)
.accept(MediaType.APPLICATION_JSON)
.header("auth-key", authKey)
.exchangeToMono(rs -> {
monitor.setResponseStatusCode(rs.statusCode().value());
return rs.bodyToMono(String.class);
});
monitor.setResponseBody(response.block());
}catch (Exception exception){
System.out.println("Error monitorBeaconResource " + resource.getResourceName());
}
long endTime = System.currentTimeMillis();
monitor.setResponseTime(endTime - startTime);
repository.save(monitor);
Expand Down Expand Up @@ -218,6 +222,7 @@ private static HttpStatusCategory monitorToHttpStatusCategory(Monitor monitor) {
int statusCodeCategoryIndicator =
Integer.parseInt(String.valueOf(monitor.getResponseStatusCode()).substring(0, 1));
return switch (statusCodeCategoryIndicator) {
case 0 -> ERROR;
case 1 -> INFORMATIONAL_RESPONSE;
case 2 -> SUCCESSFUL_RESPONSE;
case 3 -> REDIRECTION_RESPONSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public enum HttpStatusCategory {
SUCCESSFUL_RESPONSE,
REDIRECTION_RESPONSE,
CLIENT_ERROR_RESPONSE,
SERVER_ERROR_RESPONSE;
SERVER_ERROR_RESPONSE,
ERROR;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public enum Country {
SK,
SI,
ES,
SE;
SE,
TR;
}

0 comments on commit 3c150c3

Please sign in to comment.