diff --git a/src/main/java/com/powsybl/ws/commons/computation/service/AbstractWorkerService.java b/src/main/java/com/powsybl/ws/commons/computation/service/AbstractWorkerService.java index 4787ad4..a363042 100644 --- a/src/main/java/com/powsybl/ws/commons/computation/service/AbstractWorkerService.java +++ b/src/main/java/com/powsybl/ws/commons/computation/service/AbstractWorkerService.java @@ -224,6 +224,13 @@ protected R run(C runContext, UUID resultUuid, AtomicReference rootR preRun(runContext); CompletableFuture future = runAsync(runContext, provider, resultUuid); R result = future == null ? null : observer.observeRun("run", runContext, future::get); + try { + postRun(runContext, rootReporter, result); + } catch (Exception e) { + // As the exception occurs after a successful run of the computation we don't want the whole computation to fail + // Then we just log the error + LOGGER.error("An error occurred after computation run", e); + } postRun(runContext, rootReporter, result); return result; } @@ -236,7 +243,13 @@ protected R run(C runContext, UUID resultUuid, AtomicReference rootR */ protected void postRun(C runContext, AtomicReference rootReportNode, R ignoredResult) { if (runContext.getReportInfos().reportUuid() != null) { - observer.observe("report.send", runContext, () -> reportService.sendReport(runContext.getReportInfos().reportUuid(), rootReportNode.get())); + observer.observe("report.send", runContext, () -> { + try { + reportService.sendReport(runContext.getReportInfos().reportUuid(), rootReportNode.get()); + } catch (Exception e) { + throw new PostRunException("An error occurred while sending reports", e); + } + }); } } diff --git a/src/main/java/com/powsybl/ws/commons/computation/service/PostRunException.java b/src/main/java/com/powsybl/ws/commons/computation/service/PostRunException.java new file mode 100644 index 0000000..31cbf69 --- /dev/null +++ b/src/main/java/com/powsybl/ws/commons/computation/service/PostRunException.java @@ -0,0 +1,7 @@ +package com.powsybl.ws.commons.computation.service; + +public class PostRunException extends RuntimeException { + public PostRunException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/powsybl/ws/commons/computation/service/ReportService.java b/src/main/java/com/powsybl/ws/commons/computation/service/ReportService.java index 6d71675..ead0167 100644 --- a/src/main/java/com/powsybl/ws/commons/computation/service/ReportService.java +++ b/src/main/java/com/powsybl/ws/commons/computation/service/ReportService.java @@ -60,12 +60,13 @@ public void sendReport(UUID reportUuid, ReportNode reportNode) { var headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); + String str; try { - String str = objectMapper.writeValueAsString(reportNode); - restTemplate.exchange(getReportServerURI() + path, HttpMethod.PUT, new HttpEntity<>(str, headers), ReportNode.class); + str = objectMapper.writeValueAsString(reportNode); } catch (JsonProcessingException error) { - throw new PowsyblException("Error sending report", error); + throw new PowsyblException("Impossible to serialize ReportNode", error); } + restTemplate.exchange(getReportServerURI() + path, HttpMethod.PUT, new HttpEntity<>(str, headers), ReportNode.class); } public void deleteReport(UUID reportUuid) {