killbill-aplcache

jaxrs: revisit exception handling for RefundResource Signed-off-by:

7/18/2012 4:46:06 PM

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/mappers/PaymentApiExceptionMapper.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/mappers/PaymentApiExceptionMapper.java
index baa3d15..53fdfb4 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/mappers/PaymentApiExceptionMapper.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/mappers/PaymentApiExceptionMapper.java
@@ -16,17 +16,88 @@
 
 package com.ning.billing.jaxrs.mappers;
 
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.Provider;
 
+import com.ning.billing.ErrorCode;
 import com.ning.billing.payment.api.PaymentApiException;
 
 @Provider
 public class PaymentApiExceptionMapper extends ExceptionMapperBase implements ExceptionMapper<PaymentApiException> {
 
+    private final UriInfo uriInfo;
+
+    public PaymentApiExceptionMapper(@Context final UriInfo uriInfo) {
+        this.uriInfo = uriInfo;
+    }
+
     @Override
     public Response toResponse(final PaymentApiException exception) {
-        return null;
+        if (exception.getCode() == ErrorCode.PAYMENT_ADD_PAYMENT_METHOD.getCode()) {
+            return buildBadRequestResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_AMOUNT_DENIED.getCode()) {
+            return buildBadRequestResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_BAD_ACCOUNT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_CREATE_PAYMENT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_CREATE_PAYMENT_FOR_ATTEMPT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_CREATE_PAYMENT_FOR_ATTEMPT_BAD.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_CREATE_PAYMENT_FOR_ATTEMPT_WITH_NON_POSITIVE_INV.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_CREATE_PAYMENT_PROVIDER_ACCOUNT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_CREATE_REFUND.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_DEL_DEFAULT_PAYMENT_METHOD.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_DEL_PAYMENT_METHOD.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_GET_PAYMENT_METHODS.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_GET_PAYMENT_PROVIDER.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_GET_PAYMENT_PROVIDER_ACCOUNT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_INTERNAL_ERROR.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_NO_DEFAULT_PAYMENT_METHOD.getCode()) {
+            return buildNotFoundResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_NO_PAYMENT_METHODS.getCode()) {
+            return buildNotFoundResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT.getCode()) {
+            return buildNotFoundResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD.getCode()) {
+            return buildNotFoundResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_NO_SUCH_REFUND.getCode()) {
+            return buildNotFoundResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_NO_SUCH_SUCCESS_PAYMENT.getCode()) {
+            return buildNotFoundResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_NULL_INVOICE.getCode()) {
+            return buildBadRequestResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_PLUGIN_ACCOUNT_INIT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_PLUGIN_TIMEOUT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_REFRESH_PAYMENT_METHOD.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_REFUND_AMOUNT_NEGATIVE_OR_NULL.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_REFUND_AMOUNT_TOO_LARGE.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_UPD_GATEWAY_FAILED.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_UPD_PAYMENT_METHOD.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else if (exception.getCode() == ErrorCode.PAYMENT_UPD_PAYMENT_PROVIDER_ACCOUNT.getCode()) {
+            return buildInternalErrorResponse(exception, uriInfo);
+        } else {
+            return buildBadRequestResponse(exception, uriInfo);
+        }
     }
 }
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java
index b67fb68..fe1e3c7 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java
@@ -16,10 +16,6 @@
 
 package com.ning.billing.jaxrs.resources;
 
-import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
-
-import java.util.ArrayList;
-import java.util.List;
 import java.util.UUID;
 
 import javax.ws.rs.GET;
@@ -29,13 +25,7 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.inject.Inject;
-import com.ning.billing.ErrorCode;
 import com.ning.billing.jaxrs.json.RefundJson;
-import com.ning.billing.jaxrs.util.Context;
 import com.ning.billing.jaxrs.util.JaxrsUriBuilder;
 import com.ning.billing.payment.api.PaymentApi;
 import com.ning.billing.payment.api.PaymentApiException;
@@ -44,20 +34,20 @@ import com.ning.billing.util.api.CustomFieldUserApi;
 import com.ning.billing.util.api.TagUserApi;
 import com.ning.billing.util.dao.ObjectType;
 
+import com.google.inject.Inject;
+
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
 
 @Path(JaxrsResource.REFUNDS_PATH)
 public class RefundResource extends JaxRsResourceBase {
 
-    private static final Logger log = LoggerFactory.getLogger(RefundResource.class);
-
     private final PaymentApi paymentApi;
 
     @Inject
     public RefundResource(final JaxrsUriBuilder uriBuilder,
-            final PaymentApi paymentApi,
-            final TagUserApi tagUserApi,
-            final CustomFieldUserApi customFieldUserApi,
-            final Context context) {
+                          final PaymentApi paymentApi,
+                          final TagUserApi tagUserApi,
+                          final CustomFieldUserApi customFieldUserApi) {
         super(uriBuilder, tagUserApi, customFieldUserApi);
         this.paymentApi = paymentApi;
     }
@@ -65,17 +55,9 @@ public class RefundResource extends JaxRsResourceBase {
     @GET
     @Path("/{refundId:" + UUID_PATTERN + "}")
     @Produces(APPLICATION_JSON)
-    public Response getRefund(@PathParam("refundId") final String refundId) {
-        try {
-            Refund refund = paymentApi.getRefund(UUID.fromString(refundId));
-            return Response.status(Status.OK).entity(new RefundJson(refund)).build();
-        } catch (PaymentApiException e) {
-            if (e.getCode() == ErrorCode.PAYMENT_NO_SUCH_REFUND.getCode()) {
-                return Response.status(Status.NO_CONTENT).build();
-            } else {
-                return Response.status(Status.BAD_REQUEST).build();
-            }
-        }
+    public Response getRefund(@PathParam("refundId") final String refundId) throws PaymentApiException {
+        final Refund refund = paymentApi.getRefund(UUID.fromString(refundId));
+        return Response.status(Status.OK).entity(new RefundJson(refund)).build();
     }
 
     @Override