From 763b2f7b7f31c1f35d31c756d1e5b3946a25c3e2 Mon Sep 17 00:00:00 2001 From: Denis Lugowski Date: Sun, 2 Nov 2025 10:33:02 +0100 Subject: [PATCH] feat: Improve error handling --- .../legalconsenthub/error/ExceptionHandler.kt | 19 +++++++++++++++++++ .../resources/application-testcontainers.yaml | 1 + .../src/main/resources/application.yaml | 7 ------- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/legalconsenthub-backend/src/main/kotlin/com/betriebsratkanzlei/legalconsenthub/error/ExceptionHandler.kt b/legalconsenthub-backend/src/main/kotlin/com/betriebsratkanzlei/legalconsenthub/error/ExceptionHandler.kt index 8058cde..a550906 100644 --- a/legalconsenthub-backend/src/main/kotlin/com/betriebsratkanzlei/legalconsenthub/error/ExceptionHandler.kt +++ b/legalconsenthub-backend/src/main/kotlin/com/betriebsratkanzlei/legalconsenthub/error/ExceptionHandler.kt @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.http.ResponseEntity +import org.springframework.http.converter.HttpMessageNotReadableException import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ExceptionHandler 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 { + 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 @ExceptionHandler(UserAlreadyExistsException::class) @ResponseStatus(HttpStatus.CONFLICT) diff --git a/legalconsenthub-backend/src/main/resources/application-testcontainers.yaml b/legalconsenthub-backend/src/main/resources/application-testcontainers.yaml index 168b8f5..77af911 100644 --- a/legalconsenthub-backend/src/main/resources/application-testcontainers.yaml +++ b/legalconsenthub-backend/src/main/resources/application-testcontainers.yaml @@ -31,5 +31,6 @@ logging: springframework: security: TRACE oauth2: TRACE + web: DEBUG org.testcontainers: INFO com.github.dockerjava: WARN diff --git a/legalconsenthub-backend/src/main/resources/application.yaml b/legalconsenthub-backend/src/main/resources/application.yaml index 1adbfe0..29e183a 100644 --- a/legalconsenthub-backend/src/main/resources/application.yaml +++ b/legalconsenthub-backend/src/main/resources/application.yaml @@ -38,10 +38,3 @@ spring: jwt: issuer-uri: http://localhost:7080/realms/legalconsenthub jwk-set-uri: http://localhost:7080/realms/legalconsenthub/protocol/openid-connect/certs - -logging: - level: - org: - springframework: - security: TRACE - oauth2: TRACE