Skip to content

Commit 727dd32

Browse files
committed
add exception handling for bad request on getAccountsForPerson in nomis gateway
1 parent f86ad2c commit 727dd32

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/NomisGateway.kt

+31-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired
44
import org.springframework.beans.factory.annotation.Value
55
import org.springframework.http.HttpMethod
66
import org.springframework.stereotype.Component
7+
import org.springframework.web.reactive.function.client.WebClientResponseException
78
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper
89
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper.WebClientWrapperResponse
910
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Address
@@ -17,6 +18,7 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Sentence
1718
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.SentenceAdjustment
1819
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.SentenceKeyDates
1920
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
21+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
2022
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAccounts
2123
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAddress
2224
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAlert
@@ -310,24 +312,37 @@ class NomisGateway(
310312
prisonId: String,
311313
nomisNumber: String?,
312314
): Response<NomisAccounts?> {
313-
val result =
314-
webClient.request<NomisAccounts>(
315-
HttpMethod.GET,
316-
"/api/v1/prison/$prisonId/offenders/$nomisNumber/accounts",
317-
authenticationHeader(),
318-
UpstreamApi.NOMIS,
319-
)
320-
return when (result) {
321-
is WebClientWrapperResponse.Success -> {
322-
Response(data = result.data)
323-
}
324-
325-
is WebClientWrapperResponse.Error -> {
326-
Response(
327-
data = null,
328-
errors = result.errors,
315+
try {
316+
val result =
317+
webClient.request<NomisAccounts>(
318+
HttpMethod.GET,
319+
"/api/v1/prison/$prisonId/offenders/$nomisNumber/accounts",
320+
authenticationHeader(),
321+
UpstreamApi.NOMIS,
329322
)
323+
return when (result) {
324+
is WebClientWrapperResponse.Success -> {
325+
Response(data = result.data)
326+
}
327+
328+
is WebClientWrapperResponse.Error -> {
329+
Response(
330+
data = null,
331+
errors = result.errors,
332+
)
333+
}
330334
}
335+
} catch (e: WebClientResponseException.BadRequest) {
336+
return Response(
337+
data = null,
338+
errors =
339+
listOf(
340+
UpstreamApiError(
341+
causedBy = UpstreamApi.NOMIS,
342+
type = UpstreamApiError.Type.BAD_REQUEST,
343+
),
344+
),
345+
)
331346
}
332347
}
333348

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/GetAccountsForPersonTest.kt

+22
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,27 @@ class GetAccountsForPersonTest(
8282
response.errors.first().causedBy.shouldBe(UpstreamApi.NOMIS)
8383
response.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
8484
}
85+
86+
// it("returns null when 400 Bad Request is returned") {
87+
// nomisApiMockServer.stubNomisApiResponse(
88+
// accountsPath,
89+
// "",
90+
// HttpStatus.BAD_REQUEST,
91+
// )
92+
// val response =
93+
// shouldThrow<WebClientResponseException> {
94+
// nomisGateway.getAccountsForPerson(prisonId, nomisNumber)
95+
// }
96+
// response.statusCode.shouldBe(HttpStatus.BAD_REQUEST)
97+
// }
98+
it("returns an error when 400 Bad Request is returned because of invalid ID") {
99+
nomisApiMockServer.stubNomisApiResponse(accountsPath, "", HttpStatus.BAD_REQUEST)
100+
101+
val response = nomisGateway.getAccountsForPerson(prisonId, nomisNumber)
102+
103+
response.errors.shouldHaveSize(1)
104+
response.errors.first().causedBy.shouldBe(UpstreamApi.NOMIS)
105+
response.errors.first().type.shouldBe(UpstreamApiError.Type.BAD_REQUEST)
106+
}
85107
},
86108
)

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/probationoffendersearch/ProbationOffenderSearchGatewayTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ class ProbationOffenderSearchGatewayTest(
361361
response.statusCode.shouldBe(HttpStatus.BAD_REQUEST)
362362
}
363363

364+
// 1. Change their gateway tests to pass (but will we have still broken the endpoints)
365+
// 2. Change our code to catch the exception instead
366+
364367
it("returns null when no offenders are returned") {
365368
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
366369
"{\"pncNumber\": \"$hmppsId\"}",

0 commit comments

Comments
 (0)