Skip to content

Commit

Permalink
fix: πŸ› Avoid automatic response when there is existing Response
Browse files Browse the repository at this point in the history
Automatically log Exceptions that were not handled with right
ResponseStatus
  • Loading branch information
mattboll committed Jan 24, 2025
1 parent c54fbab commit 664f3c4
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<String> handleException(Exception e) {
public ResponseEntity<String> 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);
}
Expand Down

0 comments on commit 664f3c4

Please sign in to comment.