Skip to content

Commit 7e877df

Browse files
committed
docs update
1 parent 365a81d commit 7e877df

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

demo-book-service/src/main/java/org/springdoc/demo/services/book/exception/GlobalControllerExceptionHandler.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,36 @@
1818

1919
package org.springdoc.demo.services.book.exception;
2020

21+
import java.time.Instant;
22+
2123
import org.springframework.core.convert.ConversionFailedException;
2224
import org.springframework.http.HttpStatus;
23-
import org.springframework.http.ResponseEntity;
25+
import org.springframework.http.ProblemDetail;
2426
import org.springframework.web.bind.annotation.ExceptionHandler;
2527
import org.springframework.web.bind.annotation.ResponseStatus;
2628
import org.springframework.web.bind.annotation.RestControllerAdvice;
29+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
2730

2831
@RestControllerAdvice
29-
public class GlobalControllerExceptionHandler {
32+
public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {
3033

3134
@ExceptionHandler(ConversionFailedException.class)
3235
@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;
3542
}
3643

3744
@ExceptionHandler(BookNotFoundException.class)
3845
@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;
4152
}
4253
}

0 commit comments

Comments
 (0)