From 664f3c4bcda360931bdc4bc5161d5808469db040 Mon Sep 17 00:00:00 2001 From: Matthieu Bollot Date: Fri, 24 Jan 2025 17:04:33 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Avoid=20automatic=20respo?= =?UTF-8?q?nse=20when=20there=20is=20existing=20Response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Automatically log Exceptions that were not handled with right ResponseStatus --- .../api/front/log/GlobalExceptionHandler.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dossierfacile-api-tenant/src/main/java/fr/dossierfacile/api/front/log/GlobalExceptionHandler.java b/dossierfacile-api-tenant/src/main/java/fr/dossierfacile/api/front/log/GlobalExceptionHandler.java index 6dd39131..69c383a0 100644 --- a/dossierfacile-api-tenant/src/main/java/fr/dossierfacile/api/front/log/GlobalExceptionHandler.java +++ b/dossierfacile-api-tenant/src/main/java/fr/dossierfacile/api/front/log/GlobalExceptionHandler.java @@ -1,17 +1,22 @@ package fr.dossierfacile.api.front.log; import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; @ControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) - public ResponseEntity handleException(Exception e) { + public ResponseEntity handleException(Exception e) throws Exception { + if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { + throw e; + } log.error("Unhandled exception: ", e); return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR); }