Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/stripe/exception/ApiConnectionException.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,23 @@ public ApiConnectionException(String message) {
public ApiConnectionException(String message, Throwable e) {
super(message, null, null, 0, e);
}

/**
* Creates an {@link ApiConnectionException} with a standard message for IO errors during API
* requests.
*
* @param url the base URL that the request was made to
* @param cause the underlying IOException
* @return the exception
*/
public static ApiConnectionException create(String url, Throwable cause) {
String message =
String.format(
"IOException during API request to Stripe (%s): %s "
+ "Please check your internet connection and try again. If this problem persists,"
+ " you should check Stripe's service status at https://status.stripe.com,"
+ " or let us know at support@stripe.com.",
url, cause.getMessage());
return new ApiConnectionException(message, cause);
}
}
18 changes: 2 additions & 16 deletions src/main/java/com/stripe/net/HttpURLConnectionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ public StripeResponseStream requestStream(StripeRequest request) throws ApiConne
return new StripeResponseStream(responseCode, headers, responseStream);

} catch (IOException e) {
throw new ApiConnectionException(
String.format(
"IOException during API request to Stripe (%s): %s "
+ "Please check your internet connection and try again. If this problem persists,"
+ "you should check Stripe's service status at https://twitter.com/stripestatus,"
+ " or let us know at support@stripe.com.",
Stripe.getApiBase(), e.getMessage()),
e);
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
}

Expand All @@ -69,14 +62,7 @@ public StripeResponse request(StripeRequest request) throws ApiConnectionExcepti
try {
return responseStream.unstream();
} catch (IOException e) {
throw new ApiConnectionException(
String.format(
"IOException during API request to Stripe (%s): %s "
+ "Please check your internet connection and try again. If this problem persists,"
+ "you should check Stripe's service status at https://twitter.com/stripestatus,"
+ " or let us know at support@stripe.com.",
Stripe.getApiBase(), e.getMessage()),
e);
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,7 @@ public InputStream requestStream(ApiRequest apiRequest) throws StripeException {
try {
response = responseStream.unstream();
} catch (IOException e) {
throw new ApiConnectionException(
String.format(
"IOException during API request to Stripe (%s): %s "
+ "Please check your internet connection and try again. If this problem persists,"
+ "you should check Stripe's service status at https://twitter.com/stripestatus,"
+ " or let us know at support@stripe.com.",
Stripe.getApiBase(), e.getMessage()),
e);
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
handleError(response, apiRequest.getApiMode());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/stripe/net/RequestTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void maybeEnqueueMetrics(
return;
}

if (usage != null && usage.size() == 0) {
if (usage != null && usage.isEmpty()) {
usage = null;
}

Expand Down
27 changes: 3 additions & 24 deletions src/main/java/com/stripe/net/StripeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,7 @@ private StripeRequest(
this.headers = buildHeaders(method, this.options, this.content, apiMode);
this.apiMode = apiMode;
} catch (IOException e) {
throw new ApiConnectionException(
String.format(
"IOException during API request to Stripe (%s): %s "
+ "Please check your internet connection and try again. If this problem persists,"
+ "you should check Stripe's service status at https://twitter.com/stripestatus,"
+ " or let us know at support@stripe.com.",
Stripe.getApiBase(), e.getMessage()),
e);
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
}

Expand Down Expand Up @@ -112,14 +105,7 @@ private StripeRequest(
this.headers = buildHeaders(method, this.options, this.content, apiMode);
this.apiMode = apiMode;
} catch (IOException e) {
throw new ApiConnectionException(
String.format(
"IOException during API request to Stripe (%s): %s "
+ "Please check your internet connection and try again. If this problem persists,"
+ "you should check Stripe's service status at https://twitter.com/stripestatus,"
+ " or let us know at support@stripe.com.",
Stripe.getApiBase(), e.getMessage()),
e);
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
}

Expand Down Expand Up @@ -279,14 +265,7 @@ private static HttpContent buildContentFromString(
}

private static void handleIOException(IOException e) throws ApiConnectionException {
throw new ApiConnectionException(
String.format(
"IOException during API request to Stripe (%s): %s "
+ "Please check your internet connection and try again. If this problem persists,"
+ "you should check Stripe's service status at https://twitter.com/stripestatus,"
+ " or let us know at support@stripe.com.",
Stripe.getApiBase(), e.getMessage()),
e);
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}

private static HttpHeaders buildHeaders(
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/stripe/net/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static boolean verifyHeader(
throw new SignatureVerificationException(
"Unable to extract timestamp and signatures from header", sigHeader);
}
if (signatures.size() == 0) {
if (signatures.isEmpty()) {
throw new SignatureVerificationException(
"No signatures found with expected scheme", sigHeader);
}
Expand Down Expand Up @@ -239,11 +239,11 @@ public static String computeHmacSha256(String key, String message)
Mac hasher = Mac.getInstance("HmacSHA256");
hasher.init(new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] hash = hasher.doFinal(message.getBytes(StandardCharsets.UTF_8));
String result = "";
StringBuilder result = new StringBuilder(hash.length * 2);
for (byte b : hash) {
result += Integer.toString((b & 0xff) + 0x100, 16).substring(1);
result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
return result;
return result.toString();
}

/**
Expand Down