|
18 | 18 |
|
19 | 19 | package org.springdoc.demo.services.book.exception;
|
20 | 20 |
|
| 21 | +import java.time.Instant; |
| 22 | + |
21 | 23 | import org.springframework.core.convert.ConversionFailedException;
|
22 | 24 | import org.springframework.http.HttpStatus;
|
23 |
| -import org.springframework.http.ResponseEntity; |
| 25 | +import org.springframework.http.ProblemDetail; |
24 | 26 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
25 | 27 | import org.springframework.web.bind.annotation.ResponseStatus;
|
26 | 28 | import org.springframework.web.bind.annotation.RestControllerAdvice;
|
| 29 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
27 | 30 |
|
28 | 31 | @RestControllerAdvice
|
29 |
| -public class GlobalControllerExceptionHandler { |
| 32 | +public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler { |
30 | 33 |
|
31 | 34 | @ExceptionHandler(ConversionFailedException.class)
|
32 | 35 | @ResponseStatus(HttpStatus.BAD_REQUEST)
|
33 |
| - public ResponseEntity<String> handleConnversion(RuntimeException ex) { |
34 |
| - return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); |
| 36 | + public ProblemDetail handleConnversion(RuntimeException e) { |
| 37 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage()); |
| 38 | + problemDetail.setTitle("Bookmark Not Found"); |
| 39 | + problemDetail.setProperty("errorCategory", "Generic"); |
| 40 | + problemDetail.setProperty("timestamp", Instant.now()); |
| 41 | + return problemDetail; |
35 | 42 | }
|
36 | 43 |
|
37 | 44 | @ExceptionHandler(BookNotFoundException.class)
|
38 | 45 | @ResponseStatus(HttpStatus.NOT_FOUND)
|
39 |
| - public ResponseEntity<String> handleBookNotFound(RuntimeException ex) { |
40 |
| - return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND); |
| 46 | + public ProblemDetail handleBookNotFound(RuntimeException e) { |
| 47 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage()); |
| 48 | + problemDetail.setTitle("Book Not Found"); |
| 49 | + problemDetail.setProperty("errorCategory", "Generic"); |
| 50 | + problemDetail.setProperty("timestamp", Instant.now()); |
| 51 | + return problemDetail; |
41 | 52 | }
|
42 | 53 | }
|
0 commit comments