Improve exception-handling in "AssignmentsHandler.postWorkerReport()".

This commit is contained in:
Lampros Smyrnaios 2023-01-16 15:22:32 +02:00
parent d96d0c68cd
commit c283cb4365
1 changed files with 8 additions and 4 deletions

View File

@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -189,7 +190,7 @@ public class AssignmentsHandler {
public static boolean postWorkerReport(Long assignmentRequestCounter) public static boolean postWorkerReport(Long assignmentRequestCounter)
{ {
String postUrl = UrlsWorkerApplication.controllerBaseUrl + "urls/addWorkerReport"; String postUrl = UrlsWorkerApplication.controllerBaseUrl + "urls/addWorkerReport";
logger.info("Going to post the WorkerReport of assignment_" + assignmentRequestCounter + " to the controller-server: " + postUrl); logger.info("Going to post the WorkerReport of assignments_" + assignmentRequestCounter + " to the controller-server: " + postUrl);
try { try {
ResponseEntity<String> responseEntity = restTemplate.postForEntity(postUrl, new WorkerReport(UrlsWorkerApplication.workerId, assignmentRequestCounter, urlReports), String.class); ResponseEntity<String> responseEntity = restTemplate.postForEntity(postUrl, new WorkerReport(UrlsWorkerApplication.workerId, assignmentRequestCounter, urlReports), String.class);
@ -204,12 +205,15 @@ public class AssignmentsHandler {
} else if ( responseCode == HttpStatus.MULTI_STATUS.value() ) { } else if ( responseCode == HttpStatus.MULTI_STATUS.value() ) {
logger.warn("The submission of the WorkerReport of assignments_" + assignmentRequestCounter + " to the Controller was successful, but the full-texts' delivering failed!"); logger.warn("The submission of the WorkerReport of assignments_" + assignmentRequestCounter + " to the Controller was successful, but the full-texts' delivering failed!");
return true; return true;
} else { } else { // This does not include HTTP-5XX errors. For them an "HttpServerErrorException" is thrown.
logger.error("HTTP-Connection problem with the submission of the WorkerReport of assignment_" + assignmentRequestCounter + " to the Controller! Error-code was: " + responseCode); logger.error("HTTP-Connection problem with the submission of the WorkerReport of assignments_" + assignmentRequestCounter + " to the Controller! Error-code was: " + responseCode);
return false; return false;
} }
} catch (HttpServerErrorException hsee) {
logger.error("The Controller failed to handle the WorkerReport of assignments_" + assignmentRequestCounter + ": " + hsee.getMessage());
return false;
} catch (Exception e) { } catch (Exception e) {
logger.error("Error when submitting the WorkerReport of assignment_" + assignmentRequestCounter + " to the Controller: ", e); logger.error("Error when submitting the WorkerReport of assignments_" + assignmentRequestCounter + " to the Controller: ", e);
return false; return false;
} finally { } finally {
urlReports.clear(); // Reset, without de-allocating. urlReports.clear(); // Reset, without de-allocating.