Skip to content

Commit

Permalink
🥅 (ControllerAdive) ExceptionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ecureuill committed Jul 29, 2023
1 parent a584815 commit a87a4c5
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> handleEntityNotFoundException(EntityNotFoundException e) {
return ResponseEntity.notFound().build();
}

@ExceptionHandler(value = ResponseStatusException.class)
public ResponseEntity<String> handleResponseStatusException(ResponseStatusException ex) {
return ResponseEntity.status(ex.getStatusCode()).body(ex.getReason());
}

@ExceptionHandler(value = Exception.class)
public ResponseEntity<String> handleException(Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

0 comments on commit a87a4c5

Please sign in to comment.