Skip to content

Commit

Permalink
[SELC-4321] feat: handled http 400 status responses in feign decoder (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
empassaro authored May 23, 2024
1 parent 73beaf8 commit d7f10ff
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.pagopa.selfcare.external_api.exceptions;

public class InvalidRequestException extends RuntimeException {
public InvalidRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import feign.Response;
import feign.codec.ErrorDecoder;
import it.pagopa.selfcare.external_api.exceptions.InstitutionDoesNotExistException;
import it.pagopa.selfcare.external_api.exceptions.InvalidRequestException;
import it.pagopa.selfcare.external_api.exceptions.ResourceNotFoundException;

import java.util.Optional;

public class FeignErrorDecoder extends ErrorDecoder.Default {

@Override
Expand All @@ -14,6 +17,9 @@ public Exception decode(String methodKey, Response response) {
throw new ResourceNotFoundException(response.reason());
} else if (response.status() == 400 && response.request().httpMethod().equals(Request.HttpMethod.HEAD)) {
throw new InstitutionDoesNotExistException();
} else if(response.status() == 400) {
String errorMessage = Optional.ofNullable(response.body()).map(Object::toString).orElse(null);
throw new InvalidRequestException(errorMessage);
} else {
return super.decode(methodKey, response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import feign.Request;
import feign.Response;
import it.pagopa.selfcare.external_api.exceptions.InstitutionDoesNotExistException;
import it.pagopa.selfcare.external_api.exceptions.InvalidRequestException;
import it.pagopa.selfcare.external_api.exceptions.ResourceNotFoundException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand All @@ -23,6 +24,22 @@ class FeignErrorDecoderTest {

private Map<String, Collection<String>> headers = new LinkedHashMap<>();

@Test
void testDecodeToInvalidRequest() throws Throwable {
//given
Response response = Response.builder()
.status(400)
.reason("Invalid Request")
.request(Request.create(Request.HttpMethod.GET, "/api", Collections.emptyMap(), null, UTF_8))
.headers(headers)
.body("hello world", UTF_8)
.build();
//when
Executable executable = () -> feignDecoder.decode("", response);
//then
assertThrows(InvalidRequestException.class, executable);
}

@Test
void testDecodeToResourceNotFound() throws Throwable {
//given
Expand Down Expand Up @@ -57,7 +74,6 @@ void testDecodeToInstitutionDoesNotExistException() throws Throwable {

@ParameterizedTest
@CsvSource(value = {
"BadRequest,400",
"Unauthorized,401",
"OK,200"
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.pagopa.selfcare.external_api.core.exception.OnboardingNotAllowedException;
import it.pagopa.selfcare.external_api.core.exception.UpdateNotAllowedException;
import it.pagopa.selfcare.external_api.exceptions.InstitutionAlreadyOnboardedException;
import it.pagopa.selfcare.external_api.exceptions.InvalidRequestException;
import it.pagopa.selfcare.external_api.exceptions.ResourceNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -16,6 +17,11 @@
@Slf4j
@RestControllerAdvice
public class ExternalApiExceptionHandler {
@ExceptionHandler({InvalidRequestException.class})
ResponseEntity<Problem> handleInvalidRequestException(InvalidRequestException e) {
log.warn(e.toString());
return ProblemMapper.toResponseEntity(new Problem(BAD_REQUEST, e.getMessage()));
}

@ExceptionHandler(ResourceNotFoundException.class)
ResponseEntity<Problem> handleNotFoundException(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.pagopa.selfcare.external_api.core.exception.OnboardingNotAllowedException;
import it.pagopa.selfcare.external_api.core.exception.UpdateNotAllowedException;
import it.pagopa.selfcare.external_api.exceptions.InstitutionAlreadyOnboardedException;
import it.pagopa.selfcare.external_api.exceptions.InvalidRequestException;
import it.pagopa.selfcare.external_api.exceptions.ResourceNotFoundException;
import org.junit.jupiter.api.Test;
import org.springframework.http.ResponseEntity;
Expand All @@ -23,6 +24,21 @@ public ExternalApiExceptionHandlerTest() {
this.handler = new ExternalApiExceptionHandler();
}

@Test
void handleInvalidRequestException() {
//given
InvalidRequestException exceptionMock = mock(InvalidRequestException.class);
when(exceptionMock.getMessage())
.thenReturn(DETAIL_MESSAGE);
//when
ResponseEntity<Problem> responseEntity = handler.handleInvalidRequestException(exceptionMock);
//then
assertNotNull(responseEntity);
assertEquals(BAD_REQUEST, responseEntity.getStatusCode());
assertNotNull(responseEntity.getBody());
assertEquals(DETAIL_MESSAGE, responseEntity.getBody().getDetail());
assertEquals(BAD_REQUEST.value(), responseEntity.getBody().getStatus());
}

@Test
void handleResourceNotFoundException() {
Expand Down

0 comments on commit d7f10ff

Please sign in to comment.