revised exception handling for empty fields.
This commit is contained in:
@@ -5,6 +5,8 @@ import com.vaessl.app.dto.ConnectionResponse;
|
||||
|
||||
public interface ConnectionProvider {
|
||||
|
||||
void checkCredentials(ConnectionRequest request);
|
||||
|
||||
String getServiceType();
|
||||
|
||||
ConnectionResponse authenticate(ConnectionRequest request);
|
||||
|
||||
@@ -8,7 +8,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import com.vaessl.app.dto.ConnectionRequest;
|
||||
import com.vaessl.app.dto.ConnectionResponse;
|
||||
import com.vaessl.app.exception.ProviderNotFoundException;
|
||||
|
||||
@Service
|
||||
public class ConnectionService {
|
||||
@@ -27,9 +26,7 @@ public class ConnectionService {
|
||||
|
||||
ConnectionProvider provider = providerRegistry.get(request.serviceType());
|
||||
|
||||
if (provider == null) {
|
||||
throw new ProviderNotFoundException();
|
||||
}
|
||||
provider.checkCredentials(request);
|
||||
|
||||
ConnectionResponse response = provider.authenticate(request);
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.vaessl.app.connection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -9,6 +11,7 @@ import org.springframework.web.client.RestClient;
|
||||
|
||||
import com.vaessl.app.dto.ConnectionRequest;
|
||||
import com.vaessl.app.dto.ConnectionResponse;
|
||||
import com.vaessl.app.exception.EmptyCredentialsException;
|
||||
|
||||
import static com.vaessl.app.connection.Endpoint.*;
|
||||
|
||||
@@ -24,6 +27,22 @@ public class HomeBoxConnectionProvider implements ConnectionProvider {
|
||||
this.cRepository = cRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkCredentials(ConnectionRequest request) {
|
||||
if (request.username() == null || request.password() == null) {
|
||||
List<String> missingFields = new ArrayList<>();
|
||||
|
||||
if (request.username() == null) {
|
||||
missingFields.add("username");
|
||||
}
|
||||
if (request.password() == null) {
|
||||
missingFields.add("password");
|
||||
|
||||
}
|
||||
throw new EmptyCredentialsException(List.copyOf(missingFields));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return "HOMEBOX";
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.vaessl.app.exception;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class EmptyCredentialsException extends RuntimeException {
|
||||
|
||||
private final List<String> missingFields;
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.vaessl.app.exception;
|
||||
|
||||
public class ProviderNotFoundException extends RuntimeException {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user