From a87a4c5da94134ffc351af8b7109cc21466be35e Mon Sep 17 00:00:00 2001 From: ecureuill <682349-ecureuill@users.noreply.gitlab.com> Date: Fri, 28 Jul 2023 21:17:48 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20(ControllerAdive)=20ExceptionHan?= =?UTF-8?q?dler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infra/expection/ControllerAdvice.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/ecureuill/milhasapi/infra/expection/ControllerAdvice.java diff --git a/src/main/java/ecureuill/milhasapi/infra/expection/ControllerAdvice.java b/src/main/java/ecureuill/milhasapi/infra/expection/ControllerAdvice.java new file mode 100644 index 0000000..9693d71 --- /dev/null +++ b/src/main/java/ecureuill/milhasapi/infra/expection/ControllerAdvice.java @@ -0,0 +1,27 @@ +package ecureuill.milhasapi.infra.expection; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.server.ResponseStatusException; + +import jakarta.persistence.EntityNotFoundException; + +@RestControllerAdvice +public class ControllerAdvice { + + @ExceptionHandler(value = EntityNotFoundException.class) + public ResponseEntity handleEntityNotFoundException(EntityNotFoundException e) { + return ResponseEntity.notFound().build(); + } + + @ExceptionHandler(value = ResponseStatusException.class) + public ResponseEntity handleResponseStatusException(ResponseStatusException ex) { + return ResponseEntity.status(ex.getStatusCode()).body(ex.getReason()); + } + + @ExceptionHandler(value = Exception.class) + public ResponseEntity handleException(Exception e) { + return ResponseEntity.badRequest().body(e.getMessage()); + } +}