Skip to content

Commit

Permalink
[#550] Provide messageArguments and messageIf for EntityNotFoundExcep…
Browse files Browse the repository at this point in the history
…tion and CalculateException
  • Loading branch information
palagdan committed Sep 18, 2024
1 parent 6753bfb commit caa32d2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> messageArguments;


public CalculationException(String message) {
super(message);
messageId = null;
}

public CalculationException(String messageId, String message, Map<String, String> 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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ public class EntityNotFoundException extends FtaFmeaException {
private final String messageId;
private Map<String, String> messageArguments;

public EntityNotFoundException(String message, String resourceName, Object identifier) {
public EntityNotFoundException(String messageId, String message, Map<String, String> 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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public System create(System system) {
List<URI> 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;
Expand Down

0 comments on commit caa32d2

Please sign in to comment.