From caa32d267fc367e947dd0836dd1ef0e2cb1ffcae 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 | 8 ++++--- .../service/SystemRepositoryService.java | 4 ++-- 5 files changed, 31 insertions(+), 15 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..59381d68 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 @@ -33,11 +33,13 @@ public void evaluate(FaultTree faultTree){ .filter(e -> e.getProbability() == null) .toList(); if(!fe.isEmpty()){ - String message = + String leafEvents = 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); + String message = String.format("The following leaf events do not have specified probability:[\n%s]", leafEvents); + + throw new CalculationException("error.faultTree.leafEvents.noProbability", message, Map.of("leafEvents", leafEvents)); } minScenarios = null; evaluate(faultTree.getManifestingEvent()); diff --git a/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java index 1f24a8be..29c66a2b 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java @@ -73,8 +73,8 @@ public System create(System system) { List existingSystems = systemDao.findUriByName(system.getName()); if (!existingSystems.isEmpty()) throw new LogicViolationException("error.system.nameExists", - ("System with name\"%s\" already exists") - .formatted(system.getName()), + ("System with name \"%s\" already exists") + .formatted(system.getName()), Map.of("systemName", system.getName())); this.persist(system); return system;