Skip to content

Commit e25c5ee

Browse files
committed
add javadoc for solace-broker-api.exception
1 parent ba74158 commit e25c5ee

7 files changed

Lines changed: 172 additions & 0 deletions

File tree

solace-broker-api/src/main/java/org/orgname/solace/broker/api/exception/ApiExceptionHandler.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,23 @@
1515
import java.util.LinkedHashMap;
1616
import 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
1926
public 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(),

solace-broker-api/src/main/java/org/orgname/solace/broker/api/exception/BadRequestException.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
package org.orgname.solace.broker.api.exception;
22

3+
/**
4+
* Exception thrown when a request is malformed or contains invalid parameters.
5+
* <p>
6+
* This exception results in a {@code 400 Bad Request} response to the client.
7+
*/
38
public class BadRequestException extends RuntimeException {
49

10+
/**
11+
* Constructs a new {@code BadRequestException} with the specified detail message.
12+
*
13+
* @param message the detail message
14+
*/
515
public BadRequestException(String message) {
616
super(message);
717
}
818

19+
/**
20+
* Constructs a new {@code BadRequestException} with the specified detail message and cause.
21+
*
22+
* @param message the detail message
23+
* @param cause the cause of the exception
24+
*/
925
public BadRequestException(String message, Throwable cause) {
1026
super(message, cause);
1127
}

solace-broker-api/src/main/java/org/orgname/solace/broker/api/exception/BrokerConfigurationException.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
package org.orgname.solace.broker.api.exception;
22

3+
/**
4+
* Exception thrown when there is an issue with the Solace broker configuration.
5+
* <p>
6+
* This typically indicates that the application is unable to correctly interpret
7+
* or apply the provided broker settings, preventing successful integration with
8+
* the Solace PubSub+ Broker.
9+
*/
310
public class BrokerConfigurationException extends RuntimeException {
411

12+
/**
13+
* Constructs a new {@code BrokerConfigurationException} with the specified detail message.
14+
*
15+
* @param message the detail message
16+
*/
517
public BrokerConfigurationException(String message) {
618
super(message);
719
}
820

21+
/**
22+
* Constructs a new {@code BrokerConfigurationException} with the specified detail message and cause.
23+
*
24+
* @param message the detail message
25+
* @param cause the cause of the exception
26+
*/
927
public BrokerConfigurationException(String message, Throwable cause) {
1028
super(message, cause);
1129
}

solace-broker-api/src/main/java/org/orgname/solace/broker/api/exception/BrokerConnectionException.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
package org.orgname.solace.broker.api.exception;
22

3+
/**
4+
* Exception thrown when a connection to the Solace PubSub+ Broker cannot be established or is lost.
5+
* <p>
6+
* This exception indicates a connectivity issue between the API and the broker,
7+
* often resulting in a {@code 503 Service Unavailable} response.
8+
*/
39
public class BrokerConnectionException extends RuntimeException {
410

11+
/**
12+
* Constructs a new {@code BrokerConnectionException} with the specified detail message and cause.
13+
*
14+
* @param message the detail message
15+
* @param cause the cause of the exception
16+
*/
517
public BrokerConnectionException(String message, Throwable cause) {
618
super(message, cause);
719
}

solace-broker-api/src/main/java/org/orgname/solace/broker/api/exception/BrokerPublishException.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
package org.orgname.solace.broker.api.exception;
22

3+
/**
4+
* Exception thrown when an error occurs during the message publishing process to the Solace broker.
5+
* <p>
6+
* This is a general exception for publishing failures. For specific failures reported by
7+
* the broker (e.g., NACK), {@link BrokerPublishFailureException} might be used instead.
8+
* This typically results in a {@code 502 Bad Gateway} response.
9+
*/
310
public class BrokerPublishException extends RuntimeException {
411

12+
/**
13+
* Constructs a new {@code BrokerPublishException} with the specified detail message and cause.
14+
*
15+
* @param message the detail message
16+
* @param cause the cause of the exception
17+
*/
518
public BrokerPublishException(String message, Throwable cause) {
619
super(message, cause);
720
}

solace-broker-api/src/main/java/org/orgname/solace/broker/api/exception/BrokerPublishFailureException.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
package org.orgname.solace.broker.api.exception;
22

3+
/**
4+
* Exception thrown when the Solace broker explicitly rejects a message publishing attempt.
5+
* <p>
6+
* This often corresponds to a Negative Acknowledgment (NACK) from the broker, indicating
7+
* that the message was received but could not be accepted (e.g., due to permission issues,
8+
* full queues, or invalid destination).
9+
* This typically results in a {@code 502 Bad Gateway} response.
10+
*/
311
public class BrokerPublishFailureException extends RuntimeException {
412

13+
/**
14+
* Constructs a new {@code BrokerPublishFailureException} with the specified detail message and cause.
15+
*
16+
* @param message the detail message
17+
* @param cause the cause of the exception
18+
*/
519
public BrokerPublishFailureException(String message, Throwable cause) {
620
super(message, cause);
721
}

solace-broker-api/src/main/java/org/orgname/solace/broker/api/exception/ErrorMessage.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,44 @@
77
import java.time.Instant;
88
import java.util.Map;
99

10+
/**
11+
* Represents a standardized error response body for the Solace Broker API.
12+
* <p>
13+
* This class captures essential details about an error, including the timestamp,
14+
* HTTP status, a descriptive message, the request path, and any specific validation errors.
15+
*/
1016
@NoArgsConstructor
1117
@AllArgsConstructor
1218
@Data
1319
public class ErrorMessage {
20+
/**
21+
* The timestamp when the error occurred.
22+
*/
1423
private Instant timestamp;
24+
25+
/**
26+
* The HTTP status code (e.g., 400, 500).
27+
*/
1528
private int status;
29+
30+
/**
31+
* The short name of the HTTP error (e.g., "Bad Request").
32+
*/
1633
private String error;
34+
35+
/**
36+
* A detailed message explaining the error.
37+
*/
1738
private String message;
39+
40+
/**
41+
* The URI path where the error occurred.
42+
*/
1843
private String path;
44+
45+
/**
46+
* A map of field-specific validation errors, if applicable.
47+
* The key is the field name and the value is the error message.
48+
*/
1949
private Map<String, String> validationErrors;
2050
}

0 commit comments

Comments
 (0)