added ErrorMessages enum

This commit is contained in:
2026-03-30 22:41:41 +02:00
parent bda9391c75
commit c15c7a0d61
2 changed files with 38 additions and 5 deletions
@@ -0,0 +1,29 @@
package com.vaessl.app.exception;
import org.springframework.http.HttpStatus;
import lombok.Getter;
@Getter
public enum ErrorMessages {
BAD_REQUEST_EMPTY_FIELDS(HttpStatus.BAD_REQUEST, "Fields must not be empty."),
UNAUTHORIZED_WRONG_LOGIN(HttpStatus.UNAUTHORIZED, "Invalid username or password."),
SERVICE_UNAVAILABLE_UNREACHABLE_URL(HttpStatus.SERVICE_UNAVAILABLE, "The target URL is unreachable."),
SERVER_ERROR_GENERAL("The external app returned a server error: ");
private final HttpStatus status;
private final String message;
ErrorMessages(HttpStatus status, String message) {
this.status = status;
this.message = message;
}
ErrorMessages(String message) {
this.status = null;
this.message = message;
}
}
@@ -1,6 +1,5 @@
package com.vaessl.app.exception; package com.vaessl.app.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail; import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -9,24 +8,29 @@ import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.ResourceAccessException;
import static com.vaessl.app.exception.ErrorMessages.*;
@RestControllerAdvice @RestControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
public ProblemDetail handleEmptyCredentialInput(MethodArgumentNotValidException e) { public ProblemDetail handleEmptyCredentialInput(MethodArgumentNotValidException e) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Fields must not be empty."); return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
BAD_REQUEST_EMPTY_FIELDS.getMessage());
} }
@ExceptionHandler(HttpClientErrorException.Unauthorized.class) @ExceptionHandler(HttpClientErrorException.Unauthorized.class)
public ProblemDetail handleUnauthorizedAccess(HttpClientErrorException e) { public ProblemDetail handleUnauthorizedAccess(HttpClientErrorException e) {
return ProblemDetail.forStatusAndDetail(HttpStatus.UNAUTHORIZED, "Invalid username or password."); return ProblemDetail.forStatusAndDetail(UNAUTHORIZED_WRONG_LOGIN.getStatus(),
UNAUTHORIZED_WRONG_LOGIN.getMessage());
} }
@ExceptionHandler(ResourceAccessException.class) @ExceptionHandler(ResourceAccessException.class)
public ProblemDetail handleNoConnection(ResourceAccessException e) { public ProblemDetail handleNoConnection(ResourceAccessException e) {
return ProblemDetail.forStatusAndDetail(HttpStatus.SERVICE_UNAVAILABLE, "The target URL is unreachable."); return ProblemDetail.forStatusAndDetail(SERVICE_UNAVAILABLE_UNREACHABLE_URL.getStatus(),
SERVICE_UNAVAILABLE_UNREACHABLE_URL.getMessage());
} }
@ExceptionHandler(HttpServerErrorException.class) @ExceptionHandler(HttpServerErrorException.class)
@@ -34,6 +38,6 @@ public class GlobalExceptionHandler {
return ProblemDetail return ProblemDetail
.forStatusAndDetail(e.getStatusCode(), .forStatusAndDetail(e.getStatusCode(),
"The external app returned a server error: " + e.getStatusText()); SERVER_ERROR_GENERAL.getMessage() + e.getStatusText());
} }
} }