1515import java .util .LinkedHashMap ;
1616import java .util .Map ;
1717
18+ /**
19+ * Global exception handler for the Solace Broker API.
20+ * <p>
21+ * This class intercepts various exceptions thrown by the application and transforms them
22+ * into standardized {@link ErrorMessage} responses with appropriate HTTP status codes.
23+ * It handles validation errors, message parsing issues, and broker-specific exceptions.
24+ */
1825@ RestControllerAdvice
1926public class ApiExceptionHandler {
2027
28+ /**
29+ * Handles {@link MethodArgumentNotValidException} which occurs when request body validation fails.
30+ *
31+ * @param exception the exception containing validation results
32+ * @param request the current HTTP request
33+ * @return a {@code 400 Bad Request} response with field-level error details
34+ */
2135 @ ExceptionHandler (MethodArgumentNotValidException .class )
2236 public ResponseEntity <ErrorMessage > handleValidation (MethodArgumentNotValidException exception , HttpServletRequest request ) {
2337 Map <String , String > validationErrors = new LinkedHashMap <>();
@@ -32,6 +46,17 @@ public ResponseEntity<ErrorMessage> handleValidation(MethodArgumentNotValidExcep
3246 );
3347 }
3448
49+ /**
50+ * Handles {@link HttpMessageNotReadableException} which occurs when the request body is malformed
51+ * or contains invalid values for enums.
52+ * <p>
53+ * Specifically handles validation for {@code message.deliveryMode} and {@code message.payload.type}
54+ * when they don't match expected Solace or application-specific values.
55+ *
56+ * @param exception the exception containing parsing details
57+ * @param request the current HTTP request
58+ * @return a {@code 400 Bad Request} response with specific error details if available
59+ */
3560 @ ExceptionHandler (HttpMessageNotReadableException .class )
3661 public ResponseEntity <ErrorMessage > handleUnreadableMessage (HttpMessageNotReadableException exception , HttpServletRequest request ) {
3762 Throwable cause = exception .getCause ();
@@ -66,31 +91,75 @@ public ResponseEntity<ErrorMessage> handleUnreadableMessage(HttpMessageNotReadab
6691 return buildResponse (HttpStatus .BAD_REQUEST , "Request body could not be parsed" , request .getRequestURI (), null );
6792 }
6893
94+ /**
95+ * Handles {@link BadRequestException} thrown for custom business logic validation failures.
96+ *
97+ * @param exception the exception containing the error message
98+ * @param request the current HTTP request
99+ * @return a {@code 400 Bad Request} response
100+ */
69101 @ ExceptionHandler (BadRequestException .class )
70102 public ResponseEntity <ErrorMessage > handleBadRequest (BadRequestException exception , HttpServletRequest request ) {
71103 return buildResponse (HttpStatus .BAD_REQUEST , exception .getMessage (), request .getRequestURI (), null );
72104 }
73105
106+ /**
107+ * Handles {@link BrokerConfigurationException} thrown when there are issues with Solace broker settings.
108+ *
109+ * @param exception the exception containing configuration error details
110+ * @param request the current HTTP request
111+ * @return a {@code 500 Internal Server Error} response
112+ */
74113 @ ExceptionHandler (BrokerConfigurationException .class )
75114 public ResponseEntity <ErrorMessage > handleBrokerConfiguration (BrokerConfigurationException exception , HttpServletRequest request ) {
76115 return buildResponse (HttpStatus .INTERNAL_SERVER_ERROR , exception .getMessage (), request .getRequestURI (), null );
77116 }
78117
118+ /**
119+ * Handles {@link BrokerConnectionException} thrown when connection to Solace broker fails.
120+ *
121+ * @param exception the exception containing connection error details
122+ * @param request the current HTTP request
123+ * @return a {@code 503 Service Unavailable} response
124+ */
79125 @ ExceptionHandler (BrokerConnectionException .class )
80126 public ResponseEntity <ErrorMessage > handleBrokerConnection (BrokerConnectionException exception , HttpServletRequest request ) {
81127 return buildResponse (HttpStatus .SERVICE_UNAVAILABLE , exception .getMessage (), request .getRequestURI (), null );
82128 }
83129
130+ /**
131+ * Handles exceptions related to message publishing failures on the Solace broker.
132+ *
133+ * @param exception the exception (either {@link BrokerPublishFailureException} or {@link BrokerPublishException})
134+ * @param request the current HTTP request
135+ * @return a {@code 502 Bad Gateway} response
136+ */
84137 @ ExceptionHandler ({BrokerPublishFailureException .class , BrokerPublishException .class })
85138 public ResponseEntity <ErrorMessage > handleBrokerPublish (RuntimeException exception , HttpServletRequest request ) {
86139 return buildResponse (HttpStatus .BAD_GATEWAY , exception .getMessage (), request .getRequestURI (), null );
87140 }
88141
142+ /**
143+ * Fallback handler for any unexpected exceptions not specifically covered.
144+ *
145+ * @param exception the unexpected exception
146+ * @param request the current HTTP request
147+ * @return a {@code 500 Internal Server Error} response
148+ */
89149 @ ExceptionHandler (Exception .class )
90150 public ResponseEntity <ErrorMessage > handleUnexpected (Exception exception , HttpServletRequest request ) {
91151 return buildResponse (HttpStatus .INTERNAL_SERVER_ERROR , exception .getMessage (), request .getRequestURI (), null );
92152 }
93153
154+ /**
155+ * Helper method to construct the {@link ResponseEntity} with the {@link ErrorMessage} body.
156+ *
157+ * @param status the HTTP status to return
158+ * @param message the error message
159+ * @param path the request path
160+ * @param validationErrors optional map of validation errors
161+ * @return the formatted response entity
162+ */
94163 private ResponseEntity <ErrorMessage > buildResponse (HttpStatus status , String message , String path , Map <String , String > validationErrors ) {
95164 ErrorMessage body = new ErrorMessage (
96165 Instant .now (),
0 commit comments