Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localize error messages #151

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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();
}
}
4 changes: 4 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dto/error/ErrorInfo.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected GenericDao<FaultEvent> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static List<FaultEvent> 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);
Expand Down
Loading