From 177dd94c953aa4548b9cc3c6fe67881fee52c0f5 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Tue, 17 Sep 2024 22:03:35 +0200 Subject: [PATCH] [#550] Provide messageArguments and messageIf for EntityNotFoundException and CalculateException --- .../controller/util/RestExceptionHandler.java | 2 ++ .../exception/CalculationException.java | 22 +++++++++++++++---- .../exception/EntityNotFoundException.java | 10 ++++----- .../model/fta/FTAMinimalCutSetEvaluation.java | 4 ++-- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java b/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java index f269d616..a759f769 100755 --- a/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java +++ b/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java @@ -83,7 +83,9 @@ public ErrorInfo handleEvaluationException(HttpServletRequest request, Calculati log.warn("> handleEvaluationException - {}", request.getRequestURI()); return ErrorInfo.builder() .message(e.getMessage()) + .messageId(e.getMessageId()) .requestUri(request.getRequestURI()) + .messageArguments(e.getMessageArguments()) .build(); } diff --git a/src/main/java/cz/cvut/kbss/analysis/exception/CalculationException.java b/src/main/java/cz/cvut/kbss/analysis/exception/CalculationException.java index 6c96694c..7a59bea0 100755 --- a/src/main/java/cz/cvut/kbss/analysis/exception/CalculationException.java +++ b/src/main/java/cz/cvut/kbss/analysis/exception/CalculationException.java @@ -1,25 +1,39 @@ package cz.cvut.kbss.analysis.exception; import cz.cvut.kbss.analysis.model.FaultEvent; +import lombok.Getter; +import java.util.Map; + +@Getter public class CalculationException extends RuntimeException{ - public CalculationException() { - } + + private final String messageId; + private Map messageArguments; + public CalculationException(String message) { super(message); + messageId = null; + } + + public CalculationException(String messageId, String message, Map args){ + super(message); + this.messageId = messageId; + this.messageArguments = args; } public CalculationException(String message, Throwable cause) { super(message, cause); + this.messageId = null; } public static CalculationException childProbabilityNotSet(FaultEvent event){ - return new CalculationException(childProbabilityNotSetMessage(event)); + return new CalculationException("error.faultEvent.childProbabilityNotSet",childProbabilityNotSetMessage(event), Map.of("event", event.getName(), "uri", event.getUri().toString())); } public static CalculationException probabilityNotSet(FaultEvent event){ - return new CalculationException(probabilityNotSetMessage(event)); + return new CalculationException("error.faultEvent.probabilityNotSet",probabilityNotSetMessage(event), Map.of("event", event.getName(), "uri", event.getUri().toString())); } public static String probabilityNotSetMessage(FaultEvent event){ diff --git a/src/main/java/cz/cvut/kbss/analysis/exception/EntityNotFoundException.java b/src/main/java/cz/cvut/kbss/analysis/exception/EntityNotFoundException.java index d5a9288c..b96ce6cb 100755 --- a/src/main/java/cz/cvut/kbss/analysis/exception/EntityNotFoundException.java +++ b/src/main/java/cz/cvut/kbss/analysis/exception/EntityNotFoundException.java @@ -11,16 +11,14 @@ public class EntityNotFoundException extends FtaFmeaException { private final String messageId; private Map messageArguments; - public EntityNotFoundException(String message, String resourceName, Object identifier) { + public EntityNotFoundException(String messageId, String message, Map args) { super(message); - messageArguments = new HashMap<>(); - messageId = "entity_not_found"; - messageArguments.put("resourceName", resourceName); - messageArguments.put("identifier", identifier.toString()); + this.messageId = messageId; + this.messageArguments = args; } public static EntityNotFoundException create(String resourceName, Object identifier) { - return new EntityNotFoundException(resourceName + " identified by " + identifier + " not found.", resourceName, identifier); + return new EntityNotFoundException("error.entityNotFound",resourceName + " identified by " + identifier + " not found.", Map.of("resourceName", resourceName, "identifier", identifier.toString())); } } \ No newline at end of file diff --git a/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java b/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java index e6238b0c..cc74b414 100644 --- a/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java +++ b/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java @@ -35,9 +35,9 @@ public void evaluate(FaultTree faultTree){ if(!fe.isEmpty()){ String message = fe.stream().map(e -> "'%s'".formatted(e.getName())) - .collect(Collectors.joining("\n" , "The following leaf events do not have specified probability: [\n", "]")); + .collect(Collectors.joining("\n")); - throw new CalculationException(message); + throw new CalculationException("error.faultTree.leafEvents.noProbability",message, Map.of("leafEvents", message)); } minScenarios = null; evaluate(faultTree.getManifestingEvent());