Skip to content

Commit 46e5f11

Browse files
author
Bingjie Liu
committed
Refactor to thorw exception for upstream api errors
1 parent ce7ea64 commit 46e5f11

File tree

10 files changed

+295
-339
lines changed

10 files changed

+295
-339
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/HmppsIntegrationApiExceptionHandler.kt

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.springframework.http.HttpStatus.NOT_FOUND
1111
import org.springframework.http.ResponseEntity
1212
import org.springframework.web.bind.annotation.ExceptionHandler
1313
import org.springframework.web.bind.annotation.RestControllerAdvice
14+
import org.springframework.web.reactive.function.client.WebClientResponseException
1415
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.AuthenticationFailedException
1516
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException
1617

@@ -72,6 +73,20 @@ class HmppsIntegrationApiExceptionHandler {
7273
)
7374
}
7475

76+
@ExceptionHandler(WebClientResponseException::class)
77+
fun handleWebClientResponseException(e: WebClientResponseException): ResponseEntity<ErrorResponse?>? {
78+
log_and_capture("Authentication error: {}", e)
79+
return ResponseEntity
80+
.status(INTERNAL_SERVER_ERROR)
81+
.body(
82+
ErrorResponse(
83+
status = INTERNAL_SERVER_ERROR,
84+
developerMessage = "Unable to complete request as an upstream service is not responding",
85+
userMessage = e.message,
86+
),
87+
)
88+
}
89+
7590
private fun log_and_capture(message: String, e: Exception) {
7691
log.error(message, e.message)
7792
Sentry.captureException(e)

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/TomcatConfig.kt

+1-9
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.config
22

33
import org.apache.catalina.connector.Connector
44
import org.apache.tomcat.util.buf.EncodedSolidusHandling
5-
import org.springframework.beans.factory.annotation.Autowired
65
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer
76
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
87
import org.springframework.boot.web.server.WebServerFactoryCustomizer
98
import org.springframework.context.annotation.Bean
109
import org.springframework.context.annotation.Configuration
11-
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
1210
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
13-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.ServiceDownInterceptor
1411

1512
@Configuration
16-
class TomcatConfig(
17-
@Autowired val serviceDownInterceptor: ServiceDownInterceptor,
18-
) : WebMvcConfigurer {
19-
override fun addInterceptors(registry: InterceptorRegistry) {
20-
registry.addInterceptor(serviceDownInterceptor)
21-
}
13+
class TomcatConfig() : WebMvcConfigurer {
2214

2315
@Bean
2416
fun tomcatCustomizer(): WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/person/PersonController.kt

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class PersonController(
5454

5555
auditService.createEvent("SEARCH_PERSON", "Person searched with first name: $firstName, last name: $lastName, search within aliases: $searchWithinAliases, pnc number: $pncNumber, date of birth: $dateOfBirth")
5656
return response.data.paginateWith(page, perPage, response.errors)
57-
5857
}
5958

6059
@GetMapping("{encodedHmppsId}")

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/exception/AuthenticationFailedException.kt

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception
22

33
import org.springframework.http.HttpStatus
44
import org.springframework.web.bind.annotation.ResponseStatus
5-
import java.lang.RuntimeException
65

76
@ResponseStatus(HttpStatus.FORBIDDEN)
87
class AuthenticationFailedException(message: String) : RuntimeException(message)

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/ServiceDownInterceptor.kt

-26
This file was deleted.

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/WebClientWrapper.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ class WebClientWrapper(
6868
fun getErrorType(exception: WebClientResponseException, upstreamApi: UpstreamApi): WebClientWrapperResponse.Error {
6969
val errorType = when (exception.statusCode) {
7070
HttpStatus.NOT_FOUND -> UpstreamApiError.Type.ENTITY_NOT_FOUND
71-
HttpStatus.FORBIDDEN -> UpstreamApiError.Type.FORBIDDEN
72-
HttpStatus.BAD_REQUEST -> UpstreamApiError.Type.BAD_REQUEST
73-
else -> UpstreamApiError.Type.INTERNAL_SERVER_ERROR
71+
else -> throw exception
7472
}
7573
return WebClientWrapperResponse.Error(
7674
listOf(
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps
22

3-
abstract class BaseResponse (
4-
open val errors: List<UpstreamApiError> = emptyList()
3+
abstract class BaseResponse(
4+
open val errors: List<UpstreamApiError> = emptyList(),
55
) {
66

77
fun hasError(type: UpstreamApiError.Type): Boolean = this.errors.any { it.type == type }
88

99
fun hasErrorCausedBy(type: UpstreamApiError.Type, causedBy: UpstreamApi): Boolean =
1010
this.errors.any { it.type == type && it.causedBy == causedBy }
11-
1211
}

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/hmpps/Response.kt

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps
22

3-
43
data class Response<T>(val data: T, override val errors: List<UpstreamApiError> = emptyList()) : BaseResponse(errors) {
54

65
companion object {

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/util/PaginatedResponse.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.springframework.data.domain.Page
44
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.BaseResponse
55
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
66

7-
class PaginatedResponse<T>(pageableResponse: Page<T>, override val errors: List<UpstreamApiError>): BaseResponse(errors) {
7+
class PaginatedResponse<T>(pageableResponse: Page<T>, override val errors: List<UpstreamApiError>) : BaseResponse(errors) {
88

99
constructor(pageableResponse: Page<T>) : this(pageableResponse, emptyList())
1010
val data: List<T> = pageableResponse.content

0 commit comments

Comments
 (0)