killbill-aplcache

Improve exception handling

1/12/2012 3:37:50 PM

Details

diff --git a/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java b/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
index d9372d1..c056ae6 100644
--- a/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
+++ b/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
@@ -68,7 +68,7 @@ public class DefaultAccountUserApi implements com.ning.billing.account.api.Accou
     }
 
     @Override
-    public void updateAccount(final Account account) throws AccountApiException, EventBus.EventBusException {
+    public void updateAccount(final Account account) throws AccountApiException {
         dao.update(account);
     }
 }
diff --git a/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java b/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java
index 5072f90..59b8959 100644
--- a/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java
+++ b/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java
@@ -91,26 +91,33 @@ public class DefaultAccountDao implements AccountDao {
     }
 
     @Override
-    public void create(final Account account) {
+    public void create(final Account account) throws AccountApiException {
         final String key = account.getExternalKey();
+        try {
+            accountDao.inTransaction(new Transaction<Void, AccountSqlDao>() {
+                @Override
+                public Void inTransaction(final AccountSqlDao accountSqlDao, final TransactionStatus status) throws AccountApiException, EventBus.EventBusException {
+                    Account currentAccount = accountSqlDao.getAccountByKey(key);
+                    if (currentAccount != null) {
+                        throw new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, key);
+                    }
+                    accountSqlDao.create(account);
 
-        accountDao.inTransaction(new Transaction<Void, AccountSqlDao>() {
-            @Override
-            public Void inTransaction(final AccountSqlDao accountSqlDao, final TransactionStatus status) throws Exception {
-                Account currentAccount = accountSqlDao.getAccountByKey(key);
-                if (currentAccount != null) {
-                    throw new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, key);
-                }
-                accountSqlDao.create(account);
-
-                saveTagsFromWithinTransaction(account, accountSqlDao, true);
-                saveCustomFieldsFromWithinTransaction(account, accountSqlDao, true);
+                    saveTagsFromWithinTransaction(account, accountSqlDao, true);
+                    saveCustomFieldsFromWithinTransaction(account, accountSqlDao, true);
 
-                AccountCreationNotification creationEvent = new DefaultAccountCreationEvent(account);
-                eventBus.post(creationEvent);
-                return null;
+                    AccountCreationNotification creationEvent = new DefaultAccountCreationEvent(account);
+                    eventBus.post(creationEvent);
+                    return null;
+                }
+            });
+        } catch (RuntimeException re) {
+            if (re.getCause() instanceof AccountApiException) {
+                throw (AccountApiException) re.getCause();
+            } else {
+                throw re;
             }
-        });
+        }
     }
 
     @Override
@@ -142,11 +149,11 @@ public class DefaultAccountDao implements AccountDao {
                     return null;
                 }
             });
-        } catch (RuntimeException t) {
-            if (t.getCause() instanceof AccountApiException) {
-                throw (AccountApiException) t.getCause();
+        } catch (RuntimeException re) {
+            if (re.getCause() instanceof AccountApiException) {
+                throw (AccountApiException) re.getCause();
             } else {
-                throw t;
+                throw re;
             }
         }
     }
diff --git a/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java b/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java
index 649aa35..24a65ef 100644
--- a/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java
+++ b/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java
@@ -56,7 +56,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
                                    .timeZone(timeZone).build();
     }
 
-    public void testBasic() {
+    public void testBasic() throws AccountApiException {
 
         Account a = createTestAccount();
         accountDao.create(a);
@@ -95,7 +95,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
     }
 
     @Test
-    public void testCustomFields() {
+    public void testCustomFields() throws AccountApiException {
         Account account = createTestAccount();
         String fieldName = "testField1";
         String fieldValue = "testField1_value";
@@ -110,7 +110,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
     }
 
     @Test
-    public void testTags() {
+    public void testTags() throws AccountApiException {
         Account account = createTestAccount();
         TagDefinition definition = new DefaultTagDefinition("Test Tag", "For testing only", "Test System", new DateTime());
         TagDefinitionSqlDao tagDescriptionDao = dbi.onDemand(TagDefinitionSqlDao.class);
@@ -132,7 +132,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
     }
 
     @Test
-    public void testGetIdFromKey() {
+    public void testGetIdFromKey() throws AccountApiException {
         Account account = createTestAccount();
         accountDao.create(account);
 
diff --git a/api/src/main/java/com/ning/billing/account/api/AccountUserApi.java b/api/src/main/java/com/ning/billing/account/api/AccountUserApi.java
index ef86f38..9ebb876 100644
--- a/api/src/main/java/com/ning/billing/account/api/AccountUserApi.java
+++ b/api/src/main/java/com/ning/billing/account/api/AccountUserApi.java
@@ -19,6 +19,7 @@ package com.ning.billing.account.api;
 import java.util.List;
 import java.util.UUID;
 import com.ning.billing.util.customfield.CustomField;
+import com.ning.billing.util.eventbus.EventBus;
 import com.ning.billing.util.tag.Tag;
 
 public interface AccountUserApi {
@@ -30,7 +31,7 @@ public interface AccountUserApi {
      * Note: does not update the external key
      * @param account
      */
-    public void updateAccount(Account account) throws Exception;
+    public void updateAccount(Account account) throws AccountApiException;
 
     public Account getAccountByKey(String key);
 
diff --git a/util/src/main/java/com/ning/billing/util/entity/EntityDao.java b/util/src/main/java/com/ning/billing/util/entity/EntityDao.java
index cc8b45d..e308dad 100644
--- a/util/src/main/java/com/ning/billing/util/entity/EntityDao.java
+++ b/util/src/main/java/com/ning/billing/util/entity/EntityDao.java
@@ -28,10 +28,10 @@ import com.ning.billing.util.eventbus.EventBus;
 
 public interface EntityDao<T extends Entity> {
     @SqlUpdate
-    public void create(@BindBean final T entity);
+    public void create(@BindBean final T entity) throws AccountApiException;
 
     @SqlUpdate
-    public void update(@BindBean final T entity) throws AccountApiException, EventBus.EventBusException;
+    public void update(@BindBean final T entity) throws AccountApiException;
 
     @SqlQuery
     public T getById(@Bind("id") final String id);