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..46916bf 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,7 +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); - postRun(runContext, rootReporter, result); + 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); + } return result; } @@ -236,7 +242,11 @@ 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())); + try { + observer.observe("report.send", runContext, () -> 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..3faf760 --- /dev/null +++ b/src/main/java/com/powsybl/ws/commons/computation/service/PostRunException.java @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +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) {