Skip to content

Commit

Permalink
Add public models for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Feb 14, 2019
1 parent 6b10548 commit f44923c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 102 deletions.
15 changes: 3 additions & 12 deletions src/main/java/com/stripe/exception/CardException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.stripe.exception;

import lombok.Getter;

@Getter
public class CardException extends StripeException {
private static final long serialVersionUID = 2L;

Expand All @@ -17,16 +20,4 @@ public CardException(String message, String requestId, String code, String param
this.declineCode = declineCode;
this.charge = charge;
}

public String getParam() {
return param;
}

public String getDeclineCode() {
return declineCode;
}

public String getCharge() {
return charge;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.stripe.exception;

import lombok.Getter;

@Getter
public class InvalidRequestException extends StripeException {
private static final long serialVersionUID = 2L;

Expand All @@ -10,8 +13,4 @@ public InvalidRequestException(String message, String param, String requestId, S
super(message, requestId, code, statusCode, e);
this.param = param;
}

public String getParam() {
return param;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.stripe.exception;

import lombok.Getter;

@Getter
public class SignatureVerificationException extends StripeException {
private static final long serialVersionUID = 2L;

Expand All @@ -9,8 +12,4 @@ public SignatureVerificationException(String message, String sigHeader) {
super(message, null, null, 0);
this.sigHeader = sigHeader;
}

public String getSigHeader() {
return sigHeader;
}
}
21 changes: 9 additions & 12 deletions src/main/java/com/stripe/exception/StripeException.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.stripe.exception;

import com.stripe.model.StripeError;

import lombok.Getter;
import lombok.Setter;

@Getter
public abstract class StripeException extends Exception {
private static final long serialVersionUID = 2L;

@Setter
StripeError error;

private String code;
private String requestId;
private Integer statusCode;
Expand All @@ -22,18 +31,6 @@ public StripeException(String message, String requestId, String code, Integer st
this.statusCode = statusCode;
}

public String getCode() {
return code;
}

public String getRequestId() {
return requestId;
}

public Integer getStatusCode() {
return statusCode;
}

/**
* Returns a description of the exception, including the HTTP status code and request ID (if
* applicable).
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/stripe/exception/oauth/OAuthException.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.stripe.exception.oauth;

import com.stripe.exception.StripeException;
import com.stripe.model.oauth.OAuthError;

import lombok.Getter;
import lombok.Setter;

/**
* Base parent class for all OAuth exceptions.
*/
@Getter
public class OAuthException extends StripeException {
private static final long serialVersionUID = 2L;

@Setter
OAuthError oauthError;

public OAuthException(String code, String description, String requestId, Integer statusCode,
Throwable e) {
super(description, requestId, code, statusCode, e);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/stripe/model/StripeError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.stripe.model;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
public class StripeError extends StripeObject {
String charge;
String code;
String declineCode;
String docUrl;
String message;
String param;
// TODO: PaymentMethod paymentMethod
ExternalAccount source;
String type;
}
15 changes: 15 additions & 0 deletions src/main/java/com/stripe/model/oauth/OAuthError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.stripe.model.oauth;

import com.stripe.model.StripeObject;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
public class OAuthError extends StripeObject {
String error;
String errorDescription;
}
150 changes: 80 additions & 70 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stripe.net;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

import com.stripe.Stripe;
Expand All @@ -18,7 +19,9 @@
import com.stripe.exception.oauth.OAuthException;
import com.stripe.exception.oauth.UnsupportedGrantTypeException;
import com.stripe.exception.oauth.UnsupportedResponseTypeException;
import com.stripe.model.StripeError;
import com.stripe.model.StripeObject;
import com.stripe.model.oauth.OAuthError;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -374,37 +377,9 @@ private static List<Parameter> flattenParamsValue(Object value, String keyPrefix
return flatParams;
}

// represents regular API errors returned as JSON
// handleAPIError uses this class to raise the appropriate StripeException
private static class StripeErrorContainer {
private StripeError error;
}

private static class StripeError {
String type;

String message;

String code;

String param;

String declineCode;

String charge;
}

private static ConcurrentLinkedQueue<RequestMetrics> prevRequestMetrics =
new ConcurrentLinkedQueue<RequestMetrics>();

// represents OAuth API errors returned as JSON
// handleOAuthError uses this class to raise the appropriate OAuthException
private static class StripeOAuthError {
String error;

String errorDescription;
}

private static String getResponseBody(InputStream responseStream)
throws IOException {
try (final Scanner scanner = new Scanner(responseStream, ApiResource.CHARSET)) {
Expand Down Expand Up @@ -546,7 +521,7 @@ private static <T> T staticRequest(
String requestId = response.requestId();

if (responseCode < 200 || responseCode >= 300) {
handleApiError(responseBody, responseCode, requestId);
handleApiError(response);
}

T resource = null;
Expand All @@ -571,16 +546,15 @@ private static <T> T staticRequest(
private static <T> T staticOAuthRequest(
ApiResource.RequestMethod method, String url, Map<String, Object> params,
Class<T> clazz, ApiResource.RequestType type, RequestOptions options)
throws AuthenticationException, InvalidRequestException,
ApiConnectionException, ApiException, OAuthException {
throws StripeException {
StripeResponse response = rawRequest(method, url, params, type, options);

int responseCode = response.code();
String responseBody = response.body();
String requestId = response.requestId();

if (responseCode < 200 || responseCode >= 300) {
handleOAuthError(responseBody, responseCode, requestId);
handleOAuthError(response);
}

T resource = null;
Expand Down Expand Up @@ -732,72 +706,108 @@ private static void raiseMalformedJsonError(String responseBody, int responseCod
requestId, null, responseCode, null);
}

private static void handleApiError(String responseBody, int responseCode, String requestId)
throws ApiException, AuthenticationException, CardException, IdempotencyException,
InvalidRequestException {
LiveStripeResponseGetter.StripeError error = null;
private static void handleApiError(StripeResponse response)
throws StripeException {
StripeError error = null;
StripeException exception = null;

try {
error = ApiResource.GSON.fromJson(responseBody,
LiveStripeResponseGetter.StripeErrorContainer.class).error;
JsonObject jsonObject = ApiResource.GSON.fromJson(response.body(), JsonObject.class)
.getAsJsonObject("error");
error = ApiResource.GSON.fromJson(jsonObject, StripeError.class);
} catch (JsonSyntaxException e) {
raiseMalformedJsonError(responseBody, responseCode, requestId);
raiseMalformedJsonError(response.body(), response.code(), response.requestId());
}
switch (responseCode) {
error.setLastResponse(response);

switch (response.code()) {
case 400:
case 404:
if (error.type.equals("idempotency_error")) {
throw new IdempotencyException(error.message, requestId, error.code, responseCode);
if (error.getType().equals("idempotency_error")) {
exception = new IdempotencyException(error.getMessage(), response.requestId(),
error.getCode(), response.code());
} else {
throw new InvalidRequestException(error.message, error.param, requestId, error.code,
responseCode, null);
exception = new InvalidRequestException(error.getMessage(), error.getParam(),
response.requestId(), error.getCode(), response.code(), null);
}
break;
case 401:
throw new AuthenticationException(error.message, requestId, error.code, responseCode);
exception = new AuthenticationException(error.getMessage(), response.requestId(),
error.getCode(), response.code());
break;
case 402:
throw new CardException(error.message, requestId, error.code, error.param,
error.declineCode, error.charge, responseCode, null);
exception = new CardException(error.getMessage(), response.requestId(), error.getCode(),
error.getParam(), error.getDeclineCode(), error.getCharge(), response.code(), null);
break;
case 403:
throw new PermissionException(error.message, requestId, error.code, responseCode);
exception = new PermissionException(error.getMessage(), response.requestId(),
error.getCode(), response.code());
break;
case 429:
throw new RateLimitException(error.message, error.param, requestId, error.code,
responseCode, null);
exception = new RateLimitException(error.getMessage(), error.getParam(),
response.requestId(), error.getCode(), response.code(), null);
break;
default:
throw new ApiException(error.message, requestId, error.code, responseCode, null);
exception = new ApiException(error.getMessage(), response.requestId(), error.getCode(),
response.code(), null);
break;
}

exception.setError(error);

throw exception;
}

private static void handleOAuthError(String responseBody, int responseCode, String requestId)
throws InvalidClientException, InvalidGrantException,
com.stripe.exception.oauth.InvalidRequestException, InvalidScopeException,
UnsupportedGrantTypeException, UnsupportedResponseTypeException, ApiException {
LiveStripeResponseGetter.StripeOAuthError error = null;
private static void handleOAuthError(StripeResponse response)
throws StripeException {
OAuthError error = null;
StripeException exception = null;

try {
error = ApiResource.GSON.fromJson(responseBody,
LiveStripeResponseGetter.StripeOAuthError.class);
error = ApiResource.GSON.fromJson(response.body(), OAuthError.class);
} catch (JsonSyntaxException e) {
raiseMalformedJsonError(responseBody, responseCode, requestId);
raiseMalformedJsonError(response.body(), response.code(), response.requestId());
}
String code = error.error;
String description = (error.errorDescription != null) ? error.errorDescription : code;
error.setLastResponse(response);

String code = error.getError();
String description = (error.getErrorDescription() != null) ? error.getErrorDescription() : code;

switch (code) {
case "invalid_client":
throw new InvalidClientException(code, description, requestId, responseCode, null);
exception = new InvalidClientException(code, description, response.requestId(),
response.code(), null);
break;
case "invalid_grant":
throw new InvalidGrantException(code, description, requestId, responseCode, null);
exception = new InvalidGrantException(code, description, response.requestId(),
response.code(), null);
break;
case "invalid_request":
throw new com.stripe.exception.oauth.InvalidRequestException(code, description, requestId,
responseCode, null);
exception = new com.stripe.exception.oauth.InvalidRequestException(code, description,
response.requestId(), response.code(), null);
break;
case "invalid_scope":
throw new InvalidScopeException(code, description, requestId, responseCode, null);
exception = new InvalidScopeException(code, description, response.requestId(),
response.code(), null);
break;
case "unsupported_grant_type":
throw new UnsupportedGrantTypeException(code, description, requestId, responseCode, null);
exception = new UnsupportedGrantTypeException(code, description, response.requestId(),
response.code(), null);
break;
case "unsupported_response_type":
throw new UnsupportedResponseTypeException(code, description, requestId, responseCode,
null);
exception = new UnsupportedResponseTypeException(code, description, response.requestId(),
response.code(), null);
break;
default:
throw new ApiException(code, requestId, null, responseCode, null);
exception = new ApiException(code, response.requestId(), null, response.code(), null);
break;
}

if (exception instanceof OAuthException) {
((OAuthException) exception).setOauthError(error);
}

throw exception;
}

/*
Expand Down

0 comments on commit f44923c

Please sign in to comment.