Skip to content

Commit

Permalink
DSND-3213: Handle 404 from exemptions api (#167)
Browse files Browse the repository at this point in the history
* Behaviour has been changed to return an empty optional when exemptions GET returns 404. Rather than throw an exception. It's possible to have PSCs without exemptions.
  • Loading branch information
sthompsonCH authored Jan 22, 2025
1 parent 9e1f27f commit 81c7601
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import uk.gov.companieshouse.logging.Logger;
import uk.gov.companieshouse.logging.LoggerFactory;
import uk.gov.companieshouse.pscdataapi.exceptions.BadGatewayException;
import uk.gov.companieshouse.pscdataapi.exceptions.ResourceNotFoundException;
import uk.gov.companieshouse.pscdataapi.logging.DataMapHolder;

@Component
Expand All @@ -31,20 +30,17 @@ public CompanyExemptionsApiService(
}

public Optional<CompanyExemptions> getCompanyExemptions(final String companyNumber) {
ApiResponse<CompanyExemptions> response;
ApiResponse<CompanyExemptions> response = null;
try {
response = exemptionsApiClientSupplier.get()
.privateDeltaResourceHandler()
.getCompanyExemptionsResource("/company/%s/exemptions".formatted(companyNumber))
.execute();
} catch (ApiErrorResponseException ex) {
final int statusCode = ex.getStatusCode();
LOGGER.info("Company Exemptions API call failed with status code [%s]".formatted(statusCode),
LOGGER.info("Company Exemptions API GET failed with status code [%s]".formatted(statusCode),
DataMapHolder.getLogMap());
if (statusCode == 404) {
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"Company Exemptions API responded with 404 Not Found");
} else {
if (statusCode != 404) {
throw new BadGatewayException("Error calling Company Exemptions API endpoint", ex);
}
} catch (URIValidationException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void shouldGetCompanyExemptionsWithNullResponseAndReturnEmptyOptional() throws E
}

@Test
void shouldThrowResourceNotFoundExceptionWhenApiRespondsWith404NotFound() throws Exception {
void shouldReturnEmptyOptionalWhenApiRespondsWith404NotFound() throws Exception {
// given
when(supplier.get()).thenReturn(client);
when(client.privateDeltaResourceHandler()).thenReturn(privateDeltaResourceHandler);
Expand All @@ -98,11 +98,11 @@ void shouldThrowResourceNotFoundExceptionWhenApiRespondsWith404NotFound() throws
when(privateCompanyExemptionsGetAll.execute()).thenThrow(buildApiErrorResponseException(404));

// when
Executable executable = () -> service.getCompanyExemptions(COMPANY_NUMBER);
final Optional<CompanyExemptions> actual = service.getCompanyExemptions(COMPANY_NUMBER);

// then
assertThrows(ResourceNotFoundException.class, executable);
verify(privateDeltaResourceHandler).getCompanyExemptionsResource(URL);
assertEquals(Optional.empty(), actual);
}

@ParameterizedTest
Expand Down

0 comments on commit 81c7601

Please sign in to comment.