feat: Improve error handling

This commit is contained in:
2025-11-02 10:33:02 +01:00
parent 736cd17789
commit 763b2f7b7f
3 changed files with 20 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody import org.springframework.web.bind.annotation.ResponseBody
@@ -51,6 +52,24 @@ class ExceptionHandler {
) )
} }
@ResponseBody
@ExceptionHandler(HttpMessageNotReadableException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException): ResponseEntity<ProblemDetails> {
logger.warn("Failed to read HTTP message", e)
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
.body(
ProblemDetails(
title = "Bad Request",
status = HttpStatus.BAD_REQUEST.value(),
type = URI.create("about:blank"),
detail = e.message ?: "Invalid request body",
),
)
}
@ResponseBody @ResponseBody
@ExceptionHandler(UserAlreadyExistsException::class) @ExceptionHandler(UserAlreadyExistsException::class)
@ResponseStatus(HttpStatus.CONFLICT) @ResponseStatus(HttpStatus.CONFLICT)

View File

@@ -31,5 +31,6 @@ logging:
springframework: springframework:
security: TRACE security: TRACE
oauth2: TRACE oauth2: TRACE
web: DEBUG
org.testcontainers: INFO org.testcontainers: INFO
com.github.dockerjava: WARN com.github.dockerjava: WARN

View File

@@ -38,10 +38,3 @@ spring:
jwt: jwt:
issuer-uri: http://localhost:7080/realms/legalconsenthub issuer-uri: http://localhost:7080/realms/legalconsenthub
jwk-set-uri: http://localhost:7080/realms/legalconsenthub/protocol/openid-connect/certs jwk-set-uri: http://localhost:7080/realms/legalconsenthub/protocol/openid-connect/certs
logging:
level:
org:
springframework:
security: TRACE
oauth2: TRACE