-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🥅 (ControllerAdive) ExceptionHandler
- Loading branch information
ecureuill
committed
Jul 29, 2023
1 parent
a584815
commit a87a4c5
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/ecureuill/milhasapi/infra/expection/ControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |