|
| 1 | +package ai.bluefields.oidcauthdemo.error; |
| 2 | + |
| 3 | +import java.time.Instant; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.MediaType; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.web.HttpMediaTypeNotSupportedException; |
| 10 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 11 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 12 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 13 | +import org.springframework.web.servlet.NoHandlerFoundException; |
| 14 | + |
| 15 | +/** |
| 16 | + * Global exception handler for the application that converts uncaught exceptions into standardized |
| 17 | + * {@link ApiError} responses following RFC 7807 "Problem Details for HTTP APIs". |
| 18 | + * |
| 19 | + * <p>This class is responsible for: |
| 20 | + * |
| 21 | + * <ul> |
| 22 | + * <li>Catching exceptions thrown by controllers |
| 23 | + * <li>Converting them to standardized ApiError responses |
| 24 | + * <li>Setting the appropriate HTTP status code |
| 25 | + * <li>Setting the content type to "application/problem+json" |
| 26 | + * </ul> |
| 27 | + * |
| 28 | + * <p>The handler prevents internal exception details from leaking to clients while still providing |
| 29 | + * useful error information. All exceptions are logged for diagnostic purposes. |
| 30 | + * |
| 31 | + * <p>Example response: |
| 32 | + * |
| 33 | + * <pre> |
| 34 | + * { |
| 35 | + * "type": "https://api.bluefields.ai/errors/internal-error", |
| 36 | + * "title": "Internal Server Error", |
| 37 | + * "status": 500, |
| 38 | + * "detail": "An unexpected error occurred while processing your request", |
| 39 | + * "timestamp": "2025-04-17T19:08:00Z" |
| 40 | + * } |
| 41 | + * </pre> |
| 42 | + * |
| 43 | + * @see ApiError |
| 44 | + * @see <a href="https://datatracker.ietf.org/doc/html/rfc7807">RFC 7807</a> |
| 45 | + */ |
| 46 | +@RestControllerAdvice |
| 47 | +public class GlobalExceptionHandler { |
| 48 | + |
| 49 | + private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); |
| 50 | + |
| 51 | + /** |
| 52 | + * Handles all uncaught exceptions and converts them to a standardized {@link ApiError} response |
| 53 | + * with HTTP status 500 (Internal Server Error). |
| 54 | + * |
| 55 | + * <p>This method logs the exception for diagnostic purposes but returns a generic error message |
| 56 | + * to the client to avoid exposing sensitive implementation details. |
| 57 | + * |
| 58 | + * @param ex the exception that was thrown |
| 59 | + * @return a {@link ResponseEntity} containing an {@link ApiError} with status 500 |
| 60 | + */ |
| 61 | + @ExceptionHandler(Exception.class) |
| 62 | + public ResponseEntity<ApiError> handleException(Exception ex) { |
| 63 | + logger.error("Unhandled exception caught by global handler", ex); |
| 64 | + |
| 65 | + ApiError apiError = |
| 66 | + new ApiError( |
| 67 | + "https://api.bluefields.ai/errors/internal-error", |
| 68 | + "Internal Server Error", |
| 69 | + HttpStatus.INTERNAL_SERVER_ERROR.value(), |
| 70 | + "An unexpected error occurred while processing your request", |
| 71 | + Instant.now()); |
| 72 | + |
| 73 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 74 | + .contentType(MediaType.valueOf("application/problem+json")) |
| 75 | + .body(apiError); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Handles {@link NoHandlerFoundException} and converts it to a standardized {@link ApiError} |
| 80 | + * response with HTTP status 404 (Not Found). |
| 81 | + * |
| 82 | + * @param ex the exception that was thrown |
| 83 | + * @return a {@link ResponseEntity} containing an {@link ApiError} with status 404 |
| 84 | + */ |
| 85 | + @ExceptionHandler(NoHandlerFoundException.class) |
| 86 | + public ResponseEntity<ApiError> handleNoHandlerFoundException(NoHandlerFoundException ex) { |
| 87 | + logger.warn("No handler found for {}: {}", ex.getRequestURL(), ex.getMessage()); |
| 88 | + |
| 89 | + ApiError apiError = |
| 90 | + new ApiError( |
| 91 | + "https://api.bluefields.ai/errors/not-found", |
| 92 | + "Not Found", |
| 93 | + HttpStatus.NOT_FOUND.value(), |
| 94 | + "The requested resource could not be found: " + ex.getRequestURL(), |
| 95 | + Instant.now()); |
| 96 | + |
| 97 | + return ResponseEntity.status(HttpStatus.NOT_FOUND) |
| 98 | + .contentType(MediaType.valueOf("application/problem+json")) |
| 99 | + .body(apiError); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Handles {@link HttpRequestMethodNotSupportedException} and converts it to a standardized {@link |
| 104 | + * ApiError} response with HTTP status 405 (Method Not Allowed). |
| 105 | + * |
| 106 | + * @param ex the exception that was thrown |
| 107 | + * @return a {@link ResponseEntity} containing an {@link ApiError} with status 405 |
| 108 | + */ |
| 109 | + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
| 110 | + public ResponseEntity<ApiError> handleMethodNotAllowed( |
| 111 | + HttpRequestMethodNotSupportedException ex) { |
| 112 | + logger.warn("Method not allowed: {}", ex.getMessage()); |
| 113 | + |
| 114 | + ApiError apiError = |
| 115 | + new ApiError( |
| 116 | + "https://api.bluefields.ai/errors/method-not-allowed", |
| 117 | + "Method Not Allowed", |
| 118 | + HttpStatus.METHOD_NOT_ALLOWED.value(), |
| 119 | + "The HTTP method " + ex.getMethod() + " is not supported for this resource", |
| 120 | + Instant.now()); |
| 121 | + |
| 122 | + return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED) |
| 123 | + .contentType(MediaType.valueOf("application/problem+json")) |
| 124 | + .body(apiError); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Handles {@link HttpMediaTypeNotSupportedException} and converts it to a standardized {@link |
| 129 | + * ApiError} response with HTTP status 415 (Unsupported Media Type). |
| 130 | + * |
| 131 | + * @param ex the exception that was thrown |
| 132 | + * @return a {@link ResponseEntity} containing an {@link ApiError} with status 415 |
| 133 | + */ |
| 134 | + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) |
| 135 | + public ResponseEntity<ApiError> handleUnsupportedMediaType( |
| 136 | + HttpMediaTypeNotSupportedException ex) { |
| 137 | + logger.warn("Unsupported media type: {}", ex.getMessage()); |
| 138 | + |
| 139 | + ApiError apiError = |
| 140 | + new ApiError( |
| 141 | + "https://api.bluefields.ai/errors/unsupported-media-type", |
| 142 | + "Unsupported Media Type", |
| 143 | + HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), |
| 144 | + "The content type " + ex.getContentType() + " is not supported", |
| 145 | + Instant.now()); |
| 146 | + |
| 147 | + return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE) |
| 148 | + .contentType(MediaType.valueOf("application/problem+json")) |
| 149 | + .body(apiError); |
| 150 | + } |
| 151 | +} |
0 commit comments