added ErrorMessages enum
This commit is contained in:
@@ -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;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
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.ResourceAccessException;
|
||||
|
||||
import static com.vaessl.app.exception.ErrorMessages.*;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
@@ -34,6 +38,6 @@ public class GlobalExceptionHandler {
|
||||
|
||||
return ProblemDetail
|
||||
.forStatusAndDetail(e.getStatusCode(),
|
||||
"The external app returned a server error: " + e.getStatusText());
|
||||
SERVER_ERROR_GENERAL.getMessage() + e.getStatusText());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user