source: backend/src/main/java/mk/finki/roomreservation/exception/ApiExceptionHandler.java@ 09e02d7

Last change on this file since 09e02d7 was 09e02d7, checked in by Nikola Sarafimov <sarafimov.nikola12345@…>, 3 days ago

Final room reservation system implementation

  • Property mode set to 100644
File size: 1.4 KB
Line 
1package mk.finki.roomreservation.exception;
2
3import org.springframework.dao.DataAccessException;
4import org.springframework.http.HttpStatus;
5import org.springframework.http.ResponseEntity;
6import org.springframework.web.bind.annotation.ExceptionHandler;
7import org.springframework.web.bind.annotation.RestControllerAdvice;
8
9import java.util.Map;
10
11@RestControllerAdvice
12public 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.