Skip to content

Commit 42c639d

Browse files
committed
exception handling, webhhok handler
1 parent eb98d02 commit 42c639d

File tree

1,081 files changed

+104707
-103158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,081 files changed

+104707
-103158
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,29 @@ The library provides a comprehensive exception hierarchy with **strongly-typed e
221221

222222
#### Exception Hierarchy
223223

224-
All Chargebee exceptions extend from `TransportException`, which is a checked exception:
224+
All Chargebee exceptions extend from `ChargebeeException`, which is an unchecked exception:
225225

226226
```
227-
TransportException (checked)
228-
└── HttpException (HTTP status code errors: 4xx, 5xx)
229-
├── ClientErrorException (4xx errors)
230-
├── ServerErrorException (5xx errors)
231-
└── APIException (Chargebee API errors)
232-
├── InvalidRequestException (validation errors)
233-
├── PaymentException (payment-related errors)
234-
├── OperationFailedException (business logic errors)
235-
├── BatchAPIException (batch operation errors)
236-
└── UbbBatchIngestionInvalidRequestException (batch ingestion errors)
227+
ChargebeeException (unchecked - extends RuntimeException)
228+
├── ConfigurationException (setup/config errors)
229+
└── TransportException (runtime API errors)
230+
├── NetworkException (DNS failures, connection refused)
231+
├── TimeoutException (connect/read timeouts)
232+
└── HttpException (HTTP status code errors: 4xx, 5xx)
233+
├── ClientErrorException (4xx errors)
234+
├── ServerErrorException (5xx errors)
235+
└── APIException (Chargebee API errors)
236+
├── InvalidRequestException (validation errors)
237+
├── PaymentException (payment-related errors)
238+
├── OperationFailedException (business logic errors)
239+
├── BatchAPIException (batch operation errors)
240+
└── UbbBatchIngestionInvalidRequestException (batch ingestion errors)
237241
```
238242

239243
#### Exception Types
240244

245+
- **`ChargebeeException`**: Base exception for all SDK errors - catch-all for any Chargebee SDK error
246+
- **`ConfigurationException`**: Thrown when SDK configuration is invalid (missing API key, invalid URL, etc.)
241247
- **`TransportException`**: Base exception for all transport-layer failures (network issues, timeouts, etc.)
242248
- **`HttpException`**: Thrown for HTTP error status codes (4xx, 5xx) - contains status code and response
243249
- **`ClientErrorException`**: HTTP 4xx client errors (bad request, unauthorized, not found, etc.)

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ extra["projectLicenseUrl"] = projectLicenseUrl
3030
extra["teamMembers"] = teamMembers
3131

3232
java {
33-
sourceCompatibility = JavaVersion.VERSION_1_8
34-
targetCompatibility = JavaVersion.VERSION_1_8
33+
sourceCompatibility = JavaVersion.VERSION_11
34+
targetCompatibility = JavaVersion.VERSION_11
3535
withSourcesJar()
3636
withJavadocJar()
3737
}

src/main/java/com/chargebee/v4/client/ChargebeeClient.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.chargebee.v4.client.request.RequestContext;
44
import com.chargebee.v4.client.request.RequestInterceptor;
55
import com.chargebee.v4.client.request.RequestWrap;
6+
import com.chargebee.v4.exceptions.ChargebeeException;
67
import com.chargebee.v4.exceptions.ConfigurationException;
78
import com.chargebee.v4.exceptions.NetworkException;
89
import com.chargebee.v4.exceptions.TimeoutException;
@@ -109,9 +110,9 @@ public String getBaseUrl() {
109110
* @param path the API path (e.g., "/customers")
110111
* @param queryParams optional query parameters
111112
* @return the HTTP response
112-
* @throws Exception for network, timeout, configuration failures, or interceptor errors
113+
* @throws ChargebeeException for network, timeout, configuration failures, or interceptor errors
113114
*/
114-
public Response get(String path, Map<String, List<String>> queryParams) throws Exception {
115+
public Response get(String path, Map<String, List<String>> queryParams) throws ChargebeeException {
115116
Map<String, Object> objectParams = new HashMap<>(queryParams);
116117
String fullUrl = UrlBuilder.buildUrl(getBaseUrl(), path, objectParams);
117118
Request.Builder builder = Request.builder()
@@ -130,9 +131,9 @@ public Response get(String path, Map<String, List<String>> queryParams) throws E
130131
*
131132
* @param path the API path (e.g., "/customers")
132133
* @return the HTTP response
133-
* @throws Exception for network, timeout, configuration failures, or interceptor errors
134+
* @throws ChargebeeException for network, timeout, configuration failures, or interceptor errors
134135
*/
135-
public Response get(String path) throws Exception {
136+
public Response get(String path) throws ChargebeeException {
136137
return get(path, Collections.emptyMap());
137138
}
138139

@@ -173,9 +174,9 @@ public CompletableFuture<Response> getAsync(String path) {
173174
* @param path the API path (e.g., "/customers")
174175
* @param formData the form data to send
175176
* @return the HTTP response
176-
* @throws Exception for network, timeout, configuration failures, or interceptor errors
177+
* @throws ChargebeeException for network, timeout, configuration failures, or interceptor errors
177178
*/
178-
public Response post(String path, Map<String, Object> formData) throws Exception {
179+
public Response post(String path, Map<String, Object> formData) throws ChargebeeException {
179180
String fullUrl = UrlBuilder.buildUrl(getBaseUrl(), path, null);
180181
Request.Builder builder = Request.builder()
181182
.method("POST")
@@ -195,9 +196,9 @@ public Response post(String path, Map<String, Object> formData) throws Exception
195196
* @param path the API path (e.g., "/customers")
196197
* @param jsonData the JSON data to send
197198
* @return the HTTP response
198-
* @throws Exception for network, timeout, configuration failures, or interceptor errors
199+
* @throws ChargebeeException for network, timeout, configuration failures, or interceptor errors
199200
*/
200-
public Response postJson(String path, String jsonData) throws Exception {
201+
public Response postJson(String path, String jsonData) throws ChargebeeException {
201202
String fullUrl = UrlBuilder.buildUrl(getBaseUrl(), path, null);
202203
Request.Builder builder = Request.builder()
203204
.method("POST")
@@ -256,7 +257,7 @@ public CompletableFuture<Response> postJsonAsync(String path, String jsonData) {
256257
/**
257258
* Execute a request with optional interceptor.
258259
*/
259-
public Response executeWithInterceptor(Request request) throws Exception {
260+
public Response executeWithInterceptor(Request request) throws ChargebeeException {
260261
if (requestInterceptor != null) {
261262
RequestWrap requestWrap = new RequestWrap(this, request);
262263
return requestInterceptor.handleRequest(requestWrap);
@@ -280,7 +281,7 @@ public CompletableFuture<Response> executeWithInterceptorAsync(Request request)
280281
/**
281282
* Send a request with retry logic based on the configured RetryConfig.
282283
*/
283-
public Response sendWithRetry(Request request) throws TransportException {
284+
public Response sendWithRetry(Request request) {
284285
Request enrichedRequest = addDefaultHeaders(request);
285286

286287
Integer overrideRetries = enrichedRequest.getMaxNetworkRetriesOverride();

src/main/java/com/chargebee/v4/client/request/RequestInterceptor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.chargebee.v4.client.request;
22

3+
import com.chargebee.v4.exceptions.ChargebeeException;
34
import com.chargebee.v4.transport.Request;
45
import com.chargebee.v4.transport.Response;
56
import java.util.concurrent.CompletableFuture;
@@ -51,9 +52,9 @@ public interface RequestInterceptor {
5152
*
5253
* @param requestWrap wrapper containing the transport request and execution context
5354
* @return the transport response, either from proceeding or custom implementation
54-
* @throws Exception if request processing fails
55+
* @throws ChargebeeException if request processing fails
5556
*/
56-
Response handleRequest(RequestWrap requestWrap) throws Exception;
57+
Response handleRequest(RequestWrap requestWrap) throws ChargebeeException;
5758

5859
/**
5960
* Handle the intercepted request asynchronously.

src/main/java/com/chargebee/v4/client/request/RequestWrap.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.chargebee.v4.client.request;
22

33
import com.chargebee.v4.client.ChargebeeClient;
4+
import com.chargebee.v4.exceptions.ChargebeeException;
45
import com.chargebee.v4.transport.Request;
56
import com.chargebee.v4.transport.Response;
67
import java.util.concurrent.Callable;
@@ -95,9 +96,9 @@ public ChargebeeClient getClient() {
9596
* standard request processing after any modifications.</p>
9697
*
9798
* @return the transport response from normal execution
98-
* @throws Exception if request execution fails
99+
* @throws ChargebeeException if request execution fails
99100
*/
100-
public Response proceed() throws Exception {
101+
public Response proceed() throws ChargebeeException {
101102
return call();
102103
}
103104

@@ -120,10 +121,10 @@ public CompletableFuture<Response> proceedAsync() {
120121
* and transport layer.</p>
121122
*
122123
* @return the transport response
123-
* @throws Exception if request execution fails
124+
* @throws ChargebeeException if request execution fails
124125
*/
125126
@Override
126-
public Response call() throws Exception {
127+
public Response call() throws ChargebeeException {
127128
return client.sendWithRetry(request);
128129
}
129130
}

src/main/java/com/chargebee/v4/exceptions/APIException.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,12 @@ private String extractErrorCauseId(String jsonResponse) {
252252

253253
/**
254254
* API errors are generally not retryable as they indicate business logic issues.
255-
*
255+
*
256256
* <p>Exceptions that may warrant retry:
257+
*
257258
* <ul>
258-
* <li>429 Too Many Requests - Rate limiting</li>
259-
* <li>5xx Server errors - Temporary server issues</li>
259+
* <li>429 Too Many Requests - Rate limiting
260+
* <li>5xx Server errors - Temporary server issues
260261
* </ul>
261262
*
262263
* @return true only for 429 or 5xx status codes
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.chargebee.v4.exceptions;
2+
3+
/**
4+
* The root exception class for all errors originating from the Chargebee SDK.
5+
*
6+
* <p>{@code ChargebeeException} serves as the unified base for the SDK's exception hierarchy,
7+
* enabling developers to catch all Chargebee-related errors with a single exception type
8+
* while still allowing fine-grained handling of specific error categories.</p>
9+
*
10+
* <h2>Exception Hierarchy</h2>
11+
* <pre>
12+
* ChargebeeException
13+
* ├── {@link ConfigurationException} — Invalid SDK configuration (missing API key, malformed URL)
14+
* └── {@link TransportException} — Runtime communication errors
15+
* ├── {@link NetworkException} — Connection failures, DNS resolution errors
16+
* ├── {@link TimeoutException} — Connect or read timeout exceeded
17+
* └── {@link HttpException} — HTTP-level errors (4xx, 5xx responses)
18+
* ├── {@link ClientErrorException} — Client errors (400-499)
19+
* ├── {@link ServerErrorException} — Server errors (500-599)
20+
* └── {@link APIException} — Chargebee API errors with structured details
21+
* </pre>
22+
*
23+
* <h2>Usage Patterns</h2>
24+
*
25+
* <h3>Catch-all handling</h3>
26+
* <pre>{@code
27+
* try {
28+
* client.subscriptions().create(params);
29+
* } catch (ChargebeeException e) {
30+
* log.error("Chargebee operation failed: {}", e.getMessage());
31+
* if (e.isRetryable()) {
32+
* // Schedule retry
33+
* }
34+
* }
35+
* }</pre>
36+
*
37+
* <h3>Granular error handling</h3>
38+
* <pre>{@code
39+
* try {
40+
* client.subscriptions().create(params);
41+
* } catch (InvalidRequestException e) {
42+
* // Handle validation errors - check e.getParams() for invalid fields
43+
* } catch (PaymentException e) {
44+
* // Handle payment failures - check e.getErrorCauseId() for gateway details
45+
* } catch (NetworkException | TimeoutException e) {
46+
* // Handle transient failures - safe to retry
47+
* } catch (ChargebeeException e) {
48+
* // Fallback for unexpected SDK errors
49+
* }
50+
* }</pre>
51+
*
52+
* @see TransportException
53+
* @see ConfigurationException
54+
* @see APIException
55+
*/
56+
public class ChargebeeException extends RuntimeException {
57+
58+
/**
59+
* Constructs a new exception with the specified detail message.
60+
*
61+
* @param message the detail message describing the error condition
62+
*/
63+
public ChargebeeException(String message) {
64+
super(message);
65+
}
66+
67+
/**
68+
* Constructs a new exception with the specified detail message and cause.
69+
*
70+
* @param message the detail message describing the error condition
71+
* @param cause the underlying exception that triggered this error
72+
*/
73+
public ChargebeeException(String message, Throwable cause) {
74+
super(message, cause);
75+
}
76+
77+
/**
78+
* Indicates whether retrying the failed operation might succeed.
79+
*
80+
* <p>Subclasses override this method to provide accurate retry guidance:</p>
81+
* <ul>
82+
* <li>{@link NetworkException} — returns {@code true} (transient connectivity issues)</li>
83+
* <li>{@link TimeoutException} — returns {@code true} (temporary overload)</li>
84+
* <li>{@link ServerErrorException} — returns {@code true} for 5xx except 501</li>
85+
* <li>{@link ClientErrorException} — returns {@code true} only for 429 (rate limited)</li>
86+
* <li>{@link ConfigurationException} — returns {@code false} (requires code fix)</li>
87+
* </ul>
88+
*
89+
* @return {@code true} if the operation may succeed on retry; {@code false} otherwise
90+
*/
91+
public boolean isRetryable() {
92+
return false;
93+
}
94+
}
95+

src/main/java/com/chargebee/v4/exceptions/ConfigurationException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Exception thrown when there's an issue with SDK configuration.
55
*
6-
* <p>This is a runtime exception (unchecked) to allow fluent builder usage
6+
* <p>This extends {@link ChargebeeException} to allow fluent builder usage
77
* without requiring try-catch blocks during client setup.
88
*
99
* <p>Common causes:
@@ -28,7 +28,7 @@
2828
* <p>Note: This exception is NOT retryable as it indicates a programming error
2929
* that must be fixed in the code.
3030
*/
31-
public class ConfigurationException extends RuntimeException {
31+
public class ConfigurationException extends ChargebeeException {
3232

3333
/**
3434
* Creates a ConfigurationException with a message.

src/main/java/com/chargebee/v4/exceptions/TransportException.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
*
2020
* <p>Exception hierarchy:
2121
* <pre>
22-
* TransportException
23-
* ├── NetworkException (DNS failures, connection refused, I/O errors)
24-
* ├── TimeoutException (connect/read timeouts)
25-
* └── HttpException (HTTP status code errors: 4xx, 5xx)
26-
* ├── ClientErrorException (4xx)
27-
* ├── ServerErrorException (5xx)
28-
* └── APIException (Chargebee API errors with structured response)
22+
* ChargebeeException
23+
* └── TransportException
24+
* ├── NetworkException (DNS failures, connection refused, I/O errors)
25+
* ├── TimeoutException (connect/read timeouts)
26+
* └── HttpException (HTTP status code errors: 4xx, 5xx)
27+
* ├── ClientErrorException (4xx)
28+
* ├── ServerErrorException (5xx)
29+
* └── APIException (Chargebee API errors with structured response)
2930
* </pre>
3031
*/
31-
public class TransportException extends Exception {
32+
public class TransportException extends ChargebeeException {
3233

3334
private final Request request;
3435

0 commit comments

Comments
 (0)