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 17, 2024
1 parent 6753bfb commit 177dd94
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 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 @@ -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());
Expand Down

0 comments on commit 177dd94

Please sign in to comment.