From 4cdafd3f9d19504b5c4e67621b44e0643c0dbc24 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Mon, 26 Aug 2024 13:34:04 +0200 Subject: [PATCH] [kbss-cvut/fta-fmea-ui#550] Provide Ids for the exceptions --- .../controller/util/RestExceptionHandler.java | 43 ++++++++++++++----- .../kbss/analysis/dto/error/ErrorInfo.java | 4 ++ .../exception/BadRequestException.java | 11 +++++ .../exception/LogicViolationException.java | 14 +++++- .../security/CustomSwitchUserFilter.java | 2 +- .../service/FaultEventRepositoryService.java | 2 +- .../service/UserRepositoryService.java | 4 +- .../service/util/FaultTreeTraversalUtils.java | 2 +- 8 files changed, 66 insertions(+), 16 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 365b9659..8d48cb47 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 @@ -1,10 +1,7 @@ package cz.cvut.kbss.analysis.controller.util; import cz.cvut.kbss.analysis.dto.error.ErrorInfo; -import cz.cvut.kbss.analysis.exception.CalculationException; -import cz.cvut.kbss.analysis.exception.EntityNotFoundException; -import cz.cvut.kbss.analysis.exception.LogicViolationException; -import cz.cvut.kbss.analysis.exception.ValidationException; +import cz.cvut.kbss.analysis.exception.*; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.context.support.DefaultMessageSourceResolvable; @@ -24,21 +21,31 @@ public class RestExceptionHandler { @ResponseStatus(HttpStatus.UNAUTHORIZED) public ErrorInfo handleAuthenticationError(HttpServletRequest request, Throwable t) { log.warn("> handleAuthenticationError - {}", request.getRequestURI()); - return new ErrorInfo(t.getMessage(), request.getRequestURI()); + return ErrorInfo.builder() + .message(t.getMessage()) + .requestUri(request.getRequestURI()) + .build(); } @ExceptionHandler(EntityNotFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public ErrorInfo handleNotFoundError(HttpServletRequest request, Throwable t) { log.warn("> handleNotFoundError - {}", request.getRequestURI()); - return new ErrorInfo(t.getMessage(), request.getRequestURI()); + return ErrorInfo.builder() + .message(t.getMessage()) + .requestUri(request.getRequestURI()) + .build(); } @ExceptionHandler(LogicViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public ErrorInfo handleLogicViolationError(HttpServletRequest request, Throwable t) { + public ErrorInfo handleLogicViolationError(HttpServletRequest request, LogicViolationException e) { log.warn("> handleLogicViolationError - {}", request.getRequestURI()); - return new ErrorInfo(t.getMessage(), request.getRequestURI()); + return ErrorInfo.builder() + .message(e.getMessage()) + .messageId(e.getMessageId()) + .requestUri(request.getRequestURI()) + .build(); } @ExceptionHandler(ValidationException.class) @@ -51,13 +58,29 @@ public ErrorInfo handleValidationException(HttpServletRequest request, Validatio .map(DefaultMessageSourceResolvable::getDefaultMessage) .collect(Collectors.joining(",", "[", "]")); - return new ErrorInfo(errorMessage, request.getRequestURI()); + return ErrorInfo.builder() + .message(errorMessage) + .requestUri(request.getRequestURI()) + .build(); + } + + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ErrorInfo handleBadRequestException(HttpServletRequest request, BadRequestException e) { + return ErrorInfo.builder() + .message(e.getMessage()) + .messageId(e.getMessageId()) + .requestUri(request.getRequestURI()) + .build(); } @ExceptionHandler(CalculationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorInfo handleEvaluationException(HttpServletRequest request, CalculationException e) { log.warn("> handleEvaluationException - {}", request.getRequestURI()); - return new ErrorInfo(e.getMessage(), request.getRequestURI()); + return ErrorInfo.builder() + .message(e.getMessage()) + .requestUri(request.getRequestURI()) + .build(); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/dto/error/ErrorInfo.java b/src/main/java/cz/cvut/kbss/analysis/dto/error/ErrorInfo.java index 0f051657..b61e22f7 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dto/error/ErrorInfo.java +++ b/src/main/java/cz/cvut/kbss/analysis/dto/error/ErrorInfo.java @@ -1,16 +1,20 @@ package cz.cvut.kbss.analysis.dto.error; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor +@Builder public class ErrorInfo { private String message; + private String messageId; + private String requestUri; } diff --git a/src/main/java/cz/cvut/kbss/analysis/exception/BadRequestException.java b/src/main/java/cz/cvut/kbss/analysis/exception/BadRequestException.java index f6566200..2b7095f9 100644 --- a/src/main/java/cz/cvut/kbss/analysis/exception/BadRequestException.java +++ b/src/main/java/cz/cvut/kbss/analysis/exception/BadRequestException.java @@ -1,19 +1,30 @@ package cz.cvut.kbss.analysis.exception; +import lombok.Getter; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; /** * Generic exception for bad requests. */ +@Getter @ResponseStatus(HttpStatus.BAD_REQUEST) public class BadRequestException extends RuntimeException { + private final String messageId; + + public BadRequestException(String messageId, String message){ + super(message); + this.messageId = messageId; + } + public BadRequestException(String message) { super(message); + this.messageId = null; } public BadRequestException(String message, Throwable cause) { super(message, cause); + this.messageId = null; } } diff --git a/src/main/java/cz/cvut/kbss/analysis/exception/LogicViolationException.java b/src/main/java/cz/cvut/kbss/analysis/exception/LogicViolationException.java index fd66385a..41d112f8 100755 --- a/src/main/java/cz/cvut/kbss/analysis/exception/LogicViolationException.java +++ b/src/main/java/cz/cvut/kbss/analysis/exception/LogicViolationException.java @@ -1,13 +1,25 @@ package cz.cvut.kbss.analysis.exception; +import lombok.Getter; + +@Getter public class LogicViolationException extends RuntimeException { - public LogicViolationException() { } + + private final String messageId; + + public LogicViolationException(String messageId, String message){ + super(message); + this.messageId = messageId; + } public LogicViolationException(String message) { super(message); + messageId = null; } public LogicViolationException(String message, Throwable cause) { super(message, cause); + messageId = null; } + } diff --git a/src/main/java/cz/cvut/kbss/analysis/security/CustomSwitchUserFilter.java b/src/main/java/cz/cvut/kbss/analysis/security/CustomSwitchUserFilter.java index d28ac6f5..6ed99c8b 100644 --- a/src/main/java/cz/cvut/kbss/analysis/security/CustomSwitchUserFilter.java +++ b/src/main/java/cz/cvut/kbss/analysis/security/CustomSwitchUserFilter.java @@ -16,7 +16,7 @@ public class CustomSwitchUserFilter extends SwitchUserFilter { protected Authentication attemptSwitchUser(HttpServletRequest request) throws AuthenticationException { final Authentication switchTo = super.attemptSwitchUser(request); if (switchTo.getAuthorities().stream().anyMatch(a -> SecurityConstants.ROLE_ADMIN.equals(a.getAuthority()))) { - throw new BadRequestException("Cannot impersonate admin."); + throw new BadRequestException("error.user.impersonation.adminImpersonation","Cannot impersonate admin."); } return switchTo; } diff --git a/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java index e781a25e..607ea29c 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java @@ -57,7 +57,7 @@ protected GenericDao getPrimaryDao() { @Override protected void preRemove(FaultEvent instance) { boolean isRootEvent = faultTreeDao.isRootEvent(instance.getUri()); - if (isRootEvent) throw new LogicViolationException("Root event of tree mustn't be deleted!"); + if (isRootEvent) throw new LogicViolationException("error.faultTree.rootEvent.deleteViolation","Root event of tree mustn't be deleted!"); } @Transactional diff --git a/src/main/java/cz/cvut/kbss/analysis/service/UserRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/UserRepositoryService.java index 9d80c8db..da4c16a6 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/UserRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/UserRepositoryService.java @@ -63,12 +63,12 @@ public void updateCurrent(UserUpdateDTO userUpdate) { User currentUser = getCurrentUser(); if (!currentUser.getUri().equals(userUpdate.getUri())) { log.warn("< updateCurrent - URIs do not match! {} != {}", currentUser.getUri(), userUpdate.getUri()); - throw new LogicViolationException("User update uri does not match current user!"); + throw new LogicViolationException("error.user.update.uriMismatch","User update uri does not match current user!"); } if (!passwordEncoder.matches(userUpdate.getPassword(), currentUser.getPassword())) { log.warn("< updateCurrent - Old password incorrect!"); - throw new LogicViolationException("Old password incorrect!"); + throw new LogicViolationException("error.user.update.incorrectOldPassword","Old password incorrect!"); } User user = userUpdate.asUser(); diff --git a/src/main/java/cz/cvut/kbss/analysis/service/util/FaultTreeTraversalUtils.java b/src/main/java/cz/cvut/kbss/analysis/service/util/FaultTreeTraversalUtils.java index 26fb6c2f..a8f43018 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/util/FaultTreeTraversalUtils.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/util/FaultTreeTraversalUtils.java @@ -46,7 +46,7 @@ public static List rootToLeafPath(FaultEvent rootEvent, URI leafEven if (child.getEventType() == FtaEventType.INTERMEDIATE) { String message = "Intermediate event must not be the end of the path!"; log.warn(message); - throw new LogicViolationException(message); + throw new LogicViolationException("error.faultTree.intermediateEventAsLeaf",message); } path.add(child);