|
Last change
on this file was 09e02d7, checked in by Nikola Sarafimov <sarafimov.nikola12345@…>, 4 days ago |
|
Final room reservation system implementation
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Rev | Line | |
|---|
| [09e02d7] | 1 | package mk.finki.roomreservation.exception;
|
|---|
| 2 |
|
|---|
| 3 | import org.springframework.dao.DataAccessException;
|
|---|
| 4 | import org.springframework.http.HttpStatus;
|
|---|
| 5 | import org.springframework.http.ResponseEntity;
|
|---|
| 6 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
|---|
| 7 | import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|---|
| 8 |
|
|---|
| 9 | import java.util.Map;
|
|---|
| 10 |
|
|---|
| 11 | @RestControllerAdvice
|
|---|
| 12 | public class ApiExceptionHandler {
|
|---|
| 13 |
|
|---|
| 14 | @ExceptionHandler(IllegalArgumentException.class)
|
|---|
| 15 | public ResponseEntity<Map<String, String>> handleIllegalArgument(IllegalArgumentException ex) {
|
|---|
| 16 | return ResponseEntity
|
|---|
| 17 | .status(HttpStatus.BAD_REQUEST)
|
|---|
| 18 | .body(Map.of("message", ex.getMessage()));
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | @ExceptionHandler(DataAccessException.class)
|
|---|
| 22 | public ResponseEntity<Map<String, String>> handleDatabaseError(DataAccessException ex) {
|
|---|
| 23 | String message = ex.getMostSpecificCause() != null
|
|---|
| 24 | ? ex.getMostSpecificCause().getMessage()
|
|---|
| 25 | : ex.getMessage();
|
|---|
| 26 |
|
|---|
| 27 | return ResponseEntity
|
|---|
| 28 | .status(HttpStatus.BAD_REQUEST)
|
|---|
| 29 | .body(Map.of("message", message));
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | @ExceptionHandler(RuntimeException.class)
|
|---|
| 33 | public ResponseEntity<Map<String, String>> handleRuntime(RuntimeException ex) {
|
|---|
| 34 | return ResponseEntity
|
|---|
| 35 | .status(HttpStatus.BAD_REQUEST)
|
|---|
| 36 | .body(Map.of("message", ex.getMessage()));
|
|---|
| 37 | }
|
|---|
| 38 | } |
|---|
Note:
See
TracBrowser
for help on using the repository browser.