use constant for property
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2024-08-07 22:53:37 +02:00
parent 5bd9ff42ec
commit 3443dcaa95

View File

@ -17,6 +17,7 @@ import java.security.SignatureException;
public class GlobalExceptionHandler public class GlobalExceptionHandler
{ {
private static final Logger LOGGER = LogManager.getLogger(GlobalExceptionHandler.class); private static final Logger LOGGER = LogManager.getLogger(GlobalExceptionHandler.class);
private static final String DESCRIPTION_PROPERTY = "description";
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ProblemDetail handleSecurityException(Exception exception) { public ProblemDetail handleSecurityException(Exception exception) {
@ -26,34 +27,34 @@ public class GlobalExceptionHandler
if (exception instanceof BadCredentialsException) { if (exception instanceof BadCredentialsException) {
errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(401), exception.getMessage()); errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(401), exception.getMessage());
errorDetail.setProperty("description", "Invalid email or password"); errorDetail.setProperty(DESCRIPTION_PROPERTY, "Invalid email or password");
return errorDetail; return errorDetail;
} }
if (exception instanceof AccountStatusException) { if (exception instanceof AccountStatusException) {
errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage()); errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage());
errorDetail.setProperty("description", "Account locked"); errorDetail.setProperty(DESCRIPTION_PROPERTY, "Account locked");
} }
if (exception instanceof AccessDeniedException) { if (exception instanceof AccessDeniedException) {
errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage()); errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage());
errorDetail.setProperty("description", "You are not authorized to access this resource"); errorDetail.setProperty(DESCRIPTION_PROPERTY, "You are not authorized to access this resource");
} }
if (exception instanceof SignatureException) { if (exception instanceof SignatureException) {
errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage()); errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage());
errorDetail.setProperty("description", "Invalid JWT signature"); errorDetail.setProperty(DESCRIPTION_PROPERTY, "Invalid JWT signature");
} }
if (exception instanceof ExpiredJwtException) { if (exception instanceof ExpiredJwtException) {
errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage()); errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403), exception.getMessage());
errorDetail.setProperty("description", "Expired JWT token"); errorDetail.setProperty(DESCRIPTION_PROPERTY, "Expired JWT token");
} }
if (errorDetail == null) { if (errorDetail == null) {
errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(500), exception.getMessage()); errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(500), exception.getMessage());
errorDetail.setProperty("description", "Internal server error"); errorDetail.setProperty(DESCRIPTION_PROPERTY, "Internal server error");
} }
return errorDetail; return errorDetail;