revised exception handling for empty fields.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.vaessl.app.exception;
|
||||
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
@@ -10,13 +11,18 @@ import org.springframework.web.client.ResourceAccessException;
|
||||
|
||||
import static com.vaessl.app.exception.ErrorMessage.*;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ProblemDetail handleEmptyCredentialInput(MethodArgumentNotValidException e) {
|
||||
|
||||
String defaultMessages = e.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining(", "));
|
||||
|
||||
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
||||
BAD_REQUEST_EMPTY_FIELDS.getMessage());
|
||||
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " [" + defaultMessages + "]");
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpClientErrorException.Unauthorized.class)
|
||||
@@ -41,8 +47,9 @@ public class GlobalExceptionHandler {
|
||||
SERVER_ERROR_GENERAL.getMessage() + e.getStatusText());
|
||||
}
|
||||
|
||||
@ExceptionHandler(ProviderNotFoundException.class)
|
||||
public ProblemDetail handleWrongServiceType(ProviderNotFoundException e) {
|
||||
return ProblemDetail.forStatusAndDetail(WRONG_SERVICE_TYPE.getStatus(), WRONG_SERVICE_TYPE.getMessage());
|
||||
@ExceptionHandler(EmptyCredentialsException.class)
|
||||
public ProblemDetail handleEmptyCredentials(EmptyCredentialsException e) {
|
||||
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
||||
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " " + e.getMissingFields());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user