diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java b/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java index 03f46b8..a0d0530 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java @@ -11,35 +11,77 @@ import java.util.Map; import java.util.Optional; +/** + * A custom exception class for handling HTTP client-related errors and providing detailed + * information about HTTP responses and requests. This exception extends RuntimeException and can be + * used to capture and represent various HTTP communication errors with additional context. + */ public class CleverClientException extends RuntimeException { + /** + * Stores HTTP response information associated with this exception. + */ private final HttpResponseInfo responseInfo; + /** + * Constructs a new CleverClientException with a simple error message. + * + * @param message A descriptive error message + */ public CleverClientException(String message) { super(message); this.responseInfo = null; } + /** + * Constructs a new CleverClientException with a cause. + * + * @param cause The original exception that triggered this CleverClientException + */ public CleverClientException(Throwable cause) { super(cause); this.responseInfo = null; } + /** + * Constructs a new CleverClientException with a formatted message and a cause. + * + * @param message A message template with placeholders for dynamic values + * @param parameters An array of parameters to format the message, with the last parameter being the + * original cause + */ public CleverClientException(String message, Object... parameters) { super(MessageFormat.format(message, Arrays.copyOfRange(parameters, 0, parameters.length - 1)), (Throwable) parameters[parameters.length - 1]); this.responseInfo = null; } + /** + * Constructs a new CleverClientException with HTTP response information. + * + * @param responseInfo Detailed information about the HTTP response + */ public CleverClientException(HttpResponseInfo responseInfo) { super(MessageFormat.format(Constant.HTTP_ERROR_MESSAGE, responseInfo.getStatusCode()), null); this.responseInfo = responseInfo; } + /** + * Retrieves the associated HTTP response information as an Optional. + * + * @return An Optional containing HttpResponseInfo if available, otherwise empty + */ public Optional responseInfo() { return Optional.ofNullable(responseInfo); } + /** + * Attempts to extract a CleverClientException from a given Throwable. This method checks if the + * exception or its cause is a CleverClientException. + * + * @param exception The throwable to extract the CleverClientException from + * @return An Optional containing the CleverClientException if found + */ public static Optional getFrom(Throwable exception) { if (exception instanceof CleverClientException) { return Optional.of((CleverClientException) exception); @@ -50,21 +92,42 @@ public static Optional getFrom(Throwable exception) { } } + /** + * Represents detailed information about an HTTP response. This inner class captures comprehensive + * details about an HTTP communication, including status code, response data, headers, and the + * corresponding request. + */ @Data @Builder public static class HttpResponseInfo implements Serializable { + /** The HTTP status code of the response. */ private int statusCode; + + /** The raw data received in the response. */ private String data; + + /** The headers associated with the HTTP response. */ private Map> headers; + + /** Information about the HTTP request that generated this response. */ private HttpRequestInfo request; + /** + * Represents details of an HTTP request. This nested inner class captures essential information + * about the HTTP request that was made, including method, URL, and headers. + */ @Data @Builder public static class HttpRequestInfo implements Serializable { + /** The HTTP method used for the request (GET, POST, etc.). */ private String httpMethod; + + /** The full URL of the request. */ private String url; + + /** The headers sent with the HTTP request. */ private Map> headers; }