Skip to content

Commit a87a4c5

Browse files
author
ecureuill
committed
🥅 (ControllerAdive) ExceptionHandler
1 parent a584815 commit a87a4c5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ecureuill.milhasapi.infra.expection;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.bind.annotation.RestControllerAdvice;
6+
import org.springframework.web.server.ResponseStatusException;
7+
8+
import jakarta.persistence.EntityNotFoundException;
9+
10+
@RestControllerAdvice
11+
public class ControllerAdvice {
12+
13+
@ExceptionHandler(value = EntityNotFoundException.class)
14+
public ResponseEntity<String> handleEntityNotFoundException(EntityNotFoundException e) {
15+
return ResponseEntity.notFound().build();
16+
}
17+
18+
@ExceptionHandler(value = ResponseStatusException.class)
19+
public ResponseEntity<String> handleResponseStatusException(ResponseStatusException ex) {
20+
return ResponseEntity.status(ex.getStatusCode()).body(ex.getReason());
21+
}
22+
23+
@ExceptionHandler(value = Exception.class)
24+
public ResponseEntity<String> handleException(Exception e) {
25+
return ResponseEntity.badRequest().body(e.getMessage());
26+
}
27+
}

0 commit comments

Comments
 (0)