killbill-memoizeit

Details

diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java b/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java
index 883594c..5635aaa 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java
@@ -53,70 +53,64 @@ public class PlanAligner  {
     }
 
     public TimedPhase getCurrentTimedPhaseOnCreate(SubscriptionData subscription,
-            IPlan plan, String priceList, DateTime effectiveDate) {
+            IPlan plan, String priceList, DateTime effectiveDate) throws CatalogApiException {
         return getTimedPhaseOnCreate(subscription, plan, priceList, effectiveDate, WhichPhase.CURRENT);
     }
 
     public TimedPhase getNextTimedPhaseOnCreate(SubscriptionData subscription,
-            IPlan plan, String priceList, DateTime effectiveDate) {
+            IPlan plan, String priceList, DateTime effectiveDate) throws CatalogApiException {
             return getTimedPhaseOnCreate(subscription, plan, priceList, effectiveDate, WhichPhase.NEXT);
     }
 
     public TimedPhase getCurrentTimedPhaseOnChange(SubscriptionData subscription,
-            IPlan plan, String priceList, DateTime effectiveDate) {
+            IPlan plan, String priceList, DateTime effectiveDate) throws CatalogApiException {
         return getTimedPhaseOnChange(subscription, plan, priceList, effectiveDate, WhichPhase.CURRENT);
     }
 
     public TimedPhase getNextTimedPhaseOnChange(SubscriptionData subscription,
-            IPlan plan, String priceList, DateTime effectiveDate) {
+            IPlan plan, String priceList, DateTime effectiveDate) throws CatalogApiException {
         return getTimedPhaseOnChange(subscription, plan, priceList, effectiveDate, WhichPhase.NEXT);
     }
 
 
 
     public TimedPhase getNextTimedPhase(SubscriptionData subscription,
-            IPlan plan, DateTime effectiveDate, DateTime planStartDate) {
+            IPlan plan, DateTime effectiveDate, DateTime planStartDate) throws CatalogApiException {
+
         List<TimedPhase> timedPhases = getPhaseAlignments(subscription, plan, effectiveDate, planStartDate);
         return getTimedPhase(timedPhases, effectiveDate, WhichPhase.NEXT);
     }
 
     private TimedPhase getTimedPhaseOnCreate(SubscriptionData subscription,
-            IPlan plan, String priceList, DateTime effectiveDate, WhichPhase which) {
+            IPlan plan, String priceList, DateTime effectiveDate, WhichPhase which) throws CatalogApiException {
 
         ICatalog catalog = catalogService.getCatalog();
 
-            PlanSpecifier planSpecifier = new PlanSpecifier(plan.getProduct().getName(),
-                    plan.getProduct().getCategory(),
-                    plan.getBillingPeriod(),
-                    priceList);
-
-            DateTime planStartDate = null;
-
-            //TODO fix exception handling
-            PlanAlignmentCreate alignement = null;
-			try {
-				alignement = catalog.planCreateAlignment(planSpecifier);
-			} catch (CatalogApiException e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			}
-
-            switch(alignement) {
-            case START_OF_SUBSCRIPTION:
-                planStartDate = subscription.getStartDate();
-                break;
-            case START_OF_BUNDLE:
-                planStartDate = subscription.getBundleStartDate();
-                break;
-            default:
-                throw new EntitlementError(String.format("Unknwon PlanAlignmentCreate %s", alignement));
-            }
-            List<TimedPhase> timedPhases = getPhaseAlignments(subscription, plan, effectiveDate, planStartDate);
-            return getTimedPhase(timedPhases, effectiveDate, which);
+        PlanSpecifier planSpecifier = new PlanSpecifier(plan.getProduct().getName(),
+                plan.getProduct().getCategory(),
+                plan.getBillingPeriod(),
+                priceList);
+
+        DateTime planStartDate = null;
+        PlanAlignmentCreate alignement = null;
+        alignement = catalog.planCreateAlignment(planSpecifier);
+
+        switch(alignement) {
+        case START_OF_SUBSCRIPTION:
+            planStartDate = subscription.getStartDate();
+            break;
+        case START_OF_BUNDLE:
+            planStartDate = subscription.getBundleStartDate();
+            break;
+        default:
+            throw new EntitlementError(String.format("Unknwon PlanAlignmentCreate %s", alignement));
+        }
+        List<TimedPhase> timedPhases = getPhaseAlignments(subscription, plan, effectiveDate, planStartDate);
+        return getTimedPhase(timedPhases, effectiveDate, which);
     }
 
     private TimedPhase getTimedPhaseOnChange(SubscriptionData subscription,
-            IPlan plan, String priceList, DateTime effectiveDate, WhichPhase which) {
+            IPlan plan, String priceList, DateTime effectiveDate, WhichPhase which) throws CatalogApiException {
 
         ICatalog catalog = catalogService.getCatalog();
 
@@ -137,14 +131,8 @@ public class PlanAligner  {
 
         DateTime planStartDate = null;
 
-        //TODO Correctly handle exception
         PlanAlignmentChange alignment = null;
-		try {
-			alignment = catalog.planChangeAlignment(fromPlanPhaseSpecifier, toPlanSpecifier);
-		} catch (CatalogApiException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
+        alignment = catalog.planChangeAlignment(fromPlanPhaseSpecifier, toPlanSpecifier);
         switch(alignment) {
         case START_OF_SUBSCRIPTION:
             planStartDate = subscription.getStartDate();
diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/api/user/DefaultEntitlementUserApi.java b/entitlement/src/main/java/com/ning/billing/entitlement/api/user/DefaultEntitlementUserApi.java
index 3600027..f052d49 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/api/user/DefaultEntitlementUserApi.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/api/user/DefaultEntitlementUserApi.java
@@ -84,7 +84,7 @@ public class DefaultEntitlementUserApi implements EntitlementUserApi {
 
     @Override
     public SubscriptionBundle createBundleForAccount(IAccount account, String bundleName)
-        throws EntitlementUserApiException {
+    throws EntitlementUserApiException {
         SubscriptionBundleData bundle = new SubscriptionBundleData(bundleName, account.getId());
         return dao.createSubscriptionBundle(bundle);
     }
@@ -93,70 +93,67 @@ public class DefaultEntitlementUserApi implements EntitlementUserApi {
     public Subscription createSubscription(UUID bundleId, String productName,
             BillingPeriod term, String priceList, DateTime requestedDate) throws EntitlementUserApiException {
 
-        String realPriceList = (priceList == null) ? IPriceListSet.DEFAULT_PRICELIST_NAME : priceList;
-        DateTime now = clock.getUTCNow();
-        requestedDate = (requestedDate != null) ? DefaultClock.truncateMs(requestedDate) : now;
-        if (requestedDate != null && requestedDate.isAfter(now)) {
-            throw new EntitlementUserApiException(ErrorCode.ENT_INVALID_REQUESTED_DATE, requestedDate.toString());
-        }
+        try {
+            String realPriceList = (priceList == null) ? IPriceListSet.DEFAULT_PRICELIST_NAME : priceList;
+            DateTime now = clock.getUTCNow();
+            requestedDate = (requestedDate != null) ? DefaultClock.truncateMs(requestedDate) : now;
+            if (requestedDate != null && requestedDate.isAfter(now)) {
+                throw new EntitlementUserApiException(ErrorCode.ENT_INVALID_REQUESTED_DATE, requestedDate.toString());
+            }
 
-        requestedDate = (requestedDate == null) ? now : requestedDate;
+            requestedDate = (requestedDate == null) ? now : requestedDate;
 
-        //TODO: Correctly handle exception
-        IPlan plan = null;
-		try {
-			plan = catalogService.getCatalog().findPlan(productName, term, realPriceList);
-		} catch (CatalogApiException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
 
-        if (plan == null) {
-            throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_BAD_CATALOG, productName, term, realPriceList);
-        }
+            IPlan plan = catalogService.getCatalog().findPlan(productName, term, realPriceList);
+            if (plan == null) {
+                throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_BAD_CATALOG, productName, term, realPriceList);
+            }
 
-        IPlanPhase planPhase = (plan.getInitialPhases() != null) ?
-                plan.getInitialPhases()[0] : plan.getFinalPhase();
-        if (planPhase == null) {
-            throw new EntitlementError(String.format("No initial PlanPhase for Product %s, term %s and set %s does not exist in the catalog",
-                    productName, term.toString(), realPriceList));
-        }
+            IPlanPhase planPhase = (plan.getInitialPhases() != null) ? plan.getInitialPhases()[0] : plan.getFinalPhase();
 
-        SubscriptionBundle bundle = dao.getSubscriptionBundleFromId(bundleId);
-        if (bundle == null) {
-            throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_NO_BUNDLE, bundleId);
-        }
-
-        DateTime bundleStartDate = null;
-        Subscription baseSubscription = dao.getBaseSubscription(bundleId);
+            if (planPhase == null) {
+                throw new EntitlementError(String.format("No initial PlanPhase for Product %s, term %s and set %s does not exist in the catalog",
+                        productName, term.toString(), realPriceList));
+            }
 
-        switch(plan.getProduct().getCategory()) {
-        case BASE:
-            if (baseSubscription != null) {
-                throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_BP_EXISTS, bundleId);
+            SubscriptionBundle bundle = dao.getSubscriptionBundleFromId(bundleId);
+            if (bundle == null) {
+                throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_NO_BUNDLE, bundleId);
             }
-            bundleStartDate = requestedDate;
-            break;
-        case ADD_ON:
-            if (baseSubscription == null) {
-                throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_NO_BP, bundleId);
+
+            DateTime bundleStartDate = null;
+            Subscription baseSubscription = dao.getBaseSubscription(bundleId);
+
+            switch(plan.getProduct().getCategory()) {
+            case BASE:
+                if (baseSubscription != null) {
+                    throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_BP_EXISTS, bundleId);
+                }
+                bundleStartDate = requestedDate;
+                break;
+            case ADD_ON:
+                if (baseSubscription == null) {
+                    throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_NO_BP, bundleId);
+                }
+                bundleStartDate = baseSubscription.getStartDate();
+                break;
+            default:
+                throw new EntitlementError(String.format("Can't create subscription of type %s",
+                        plan.getProduct().getCategory().toString()));
             }
-            bundleStartDate = baseSubscription.getStartDate();
-            break;
-         default:
-             throw new EntitlementError(String.format("Can't create subscription of type %s",
-                     plan.getProduct().getCategory().toString()));
-        }
 
-        DateTime effectiveDate = requestedDate;
-        SubscriptionData subscription = apiService.create(new SubscriptionBuilder()
+            DateTime effectiveDate = requestedDate;
+            SubscriptionData subscription = apiService.create(new SubscriptionBuilder()
             .setId(UUID.randomUUID())
             .setBundleId(bundleId)
             .setCategory(plan.getProduct().getCategory())
             .setBundleStartDate(bundleStartDate)
             .setStartDate(effectiveDate),
-                plan, realPriceList, requestedDate, effectiveDate, now);
+            plan, realPriceList, requestedDate, effectiveDate, now);
 
-        return subscription;
+            return subscription;
+        } catch (CatalogApiException e) {
+            throw new EntitlementUserApiException(e);
+        }
     }
 }
diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionApiService.java b/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionApiService.java
index 86483d1..9629e0f 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionApiService.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionApiService.java
@@ -68,12 +68,14 @@ public class SubscriptionApiService {
 
 
     public SubscriptionData create(SubscriptionBuilder builder, IPlan plan, String realPriceList,
-            DateTime requestedDate, DateTime effectiveDate, DateTime processedDate) {
-        SubscriptionData subscription = new SubscriptionData(builder, this, clock);
+            DateTime requestedDate, DateTime effectiveDate, DateTime processedDate)
+        throws EntitlementUserApiException {
 
+        try {
+            SubscriptionData subscription = new SubscriptionData(builder, this, clock);
 
-        TimedPhase currentTimedPhase =  planAligner.getCurrentTimedPhaseOnCreate(subscription, plan, realPriceList, effectiveDate);
-        ApiEventCreate creationEvent = new ApiEventCreate(new ApiEventBuilder()
+            TimedPhase currentTimedPhase =  planAligner.getCurrentTimedPhaseOnCreate(subscription, plan, realPriceList, effectiveDate);
+            ApiEventCreate creationEvent = new ApiEventCreate(new ApiEventBuilder()
             .setSubscriptionId(subscription.getId())
             .setEventPlan(plan.getName())
             .setEventPlanPhase(currentTimedPhase.getPhase().getName())
@@ -83,90 +85,99 @@ public class SubscriptionApiService {
             .setEffectiveDate(effectiveDate)
             .setRequestedDate(requestedDate));
 
-        TimedPhase nextTimedPhase =  planAligner.getNextTimedPhaseOnCreate(subscription, plan, realPriceList, effectiveDate);
-        PhaseEvent nextPhaseEvent = PhaseEventData.getNextPhaseEvent(nextTimedPhase, subscription, processedDate);
-        List<EntitlementEvent> events = new ArrayList<EntitlementEvent>();
-        events.add(creationEvent);
-        if (nextPhaseEvent != null) {
-            events.add(nextPhaseEvent);
+            TimedPhase nextTimedPhase =  planAligner.getNextTimedPhaseOnCreate(subscription, plan, realPriceList, effectiveDate);
+            PhaseEvent nextPhaseEvent = PhaseEventData.getNextPhaseEvent(nextTimedPhase, subscription, processedDate);
+            List<EntitlementEvent> events = new ArrayList<EntitlementEvent>();
+            events.add(creationEvent);
+            if (nextPhaseEvent != null) {
+                events.add(nextPhaseEvent);
+            }
+            dao.createSubscription(subscription, events);
+            subscription.rebuildTransitions(events, catalogService.getCatalog());
+            return subscription;
+        } catch (CatalogApiException e) {
+            throw new EntitlementUserApiException(e);
         }
-        dao.createSubscription(subscription, events);
-        subscription.rebuildTransitions(events, catalogService.getCatalog());
-        return subscription;
     }
 
     public void cancel(SubscriptionData subscription, DateTime requestedDate, boolean eot)
         throws EntitlementUserApiException {
 
-        SubscriptionState currentState = subscription.getState();
-        if (currentState != SubscriptionState.ACTIVE) {
-            throw new EntitlementUserApiException(ErrorCode.ENT_CANCEL_BAD_STATE, subscription.getId(), currentState);
-        }
+        try {
+            SubscriptionState currentState = subscription.getState();
+            if (currentState != SubscriptionState.ACTIVE) {
+                throw new EntitlementUserApiException(ErrorCode.ENT_CANCEL_BAD_STATE, subscription.getId(), currentState);
+            }
 
-        DateTime now = clock.getUTCNow();
-        requestedDate = (requestedDate != null) ? DefaultClock.truncateMs(requestedDate) : null;
-        if (requestedDate != null && requestedDate.isAfter(now)) {
-            throw new EntitlementUserApiException(ErrorCode.ENT_INVALID_REQUESTED_DATE, requestedDate.toString());
-        }
+            DateTime now = clock.getUTCNow();
+            requestedDate = (requestedDate != null) ? DefaultClock.truncateMs(requestedDate) : null;
+            if (requestedDate != null && requestedDate.isAfter(now)) {
+                throw new EntitlementUserApiException(ErrorCode.ENT_INVALID_REQUESTED_DATE, requestedDate.toString());
+            }
 
-        IPlan currentPlan = subscription.getCurrentPlan();
-        PlanPhaseSpecifier planPhase = new PlanPhaseSpecifier(currentPlan.getProduct().getName(),
-                currentPlan.getProduct().getCategory(),
-                subscription.getCurrentPlan().getBillingPeriod(),
-                subscription.getCurrentPriceList(),
-                subscription.getCurrentPhase().getPhaseType());
+            IPlan currentPlan = subscription.getCurrentPlan();
+            PlanPhaseSpecifier planPhase = new PlanPhaseSpecifier(currentPlan.getProduct().getName(),
+                    currentPlan.getProduct().getCategory(),
+                    subscription.getCurrentPlan().getBillingPeriod(),
+                    subscription.getCurrentPriceList(),
+                    subscription.getCurrentPhase().getPhaseType());
 
-        //TODO: Correctly handle exception
-        ActionPolicy policy = null;
-        try {
+            ActionPolicy policy = null;
             policy = catalogService.getCatalog().planCancelPolicy(planPhase);
-        } catch (CatalogApiException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        DateTime effectiveDate = subscription.getPlanChangeEffectiveDate(policy, now);
+            DateTime effectiveDate = subscription.getPlanChangeEffectiveDate(policy, now);
 
-        EntitlementEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder()
-        .setSubscriptionId(subscription.getId())
-        .setActiveVersion(subscription.getActiveVersion())
-        .setProcessedDate(now)
-        .setEffectiveDate(effectiveDate)
-        .setRequestedDate(now));
+            EntitlementEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder()
+            .setSubscriptionId(subscription.getId())
+            .setActiveVersion(subscription.getActiveVersion())
+            .setProcessedDate(now)
+            .setEffectiveDate(effectiveDate)
+            .setRequestedDate(now));
 
-        dao.cancelSubscription(subscription.getId(), cancelEvent);
-        subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId()), catalogService.getCatalog());
+            dao.cancelSubscription(subscription.getId(), cancelEvent);
+            subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId()), catalogService.getCatalog());
+        } catch (CatalogApiException e) {
+            throw new EntitlementUserApiException(e);
+        }
     }
 
 
-    public void uncancel(SubscriptionData subscription) throws EntitlementUserApiException {
+    public void uncancel(SubscriptionData subscription)
+        throws EntitlementUserApiException {
+
         if (!subscription.isSubscriptionFutureCancelled()) {
             throw new EntitlementUserApiException(ErrorCode.ENT_UNCANCEL_BAD_STATE, subscription.getId().toString());
         }
-        DateTime now = clock.getUTCNow();
-        EntitlementEvent uncancelEvent = new ApiEventUncancel(new ApiEventBuilder()
+
+        try {
+            DateTime now = clock.getUTCNow();
+            EntitlementEvent uncancelEvent = new ApiEventUncancel(new ApiEventBuilder()
             .setSubscriptionId(subscription.getId())
             .setActiveVersion(subscription.getActiveVersion())
             .setProcessedDate(now)
             .setRequestedDate(now)
             .setEffectiveDate(now));
 
-        List<EntitlementEvent> uncancelEvents = new ArrayList<EntitlementEvent>();
-        uncancelEvents.add(uncancelEvent);
+            List<EntitlementEvent> uncancelEvents = new ArrayList<EntitlementEvent>();
+            uncancelEvents.add(uncancelEvent);
 
-        DateTime planStartDate = subscription.getCurrentPlanStart();
-        TimedPhase nextTimedPhase = planAligner.getNextTimedPhase(subscription, subscription.getCurrentPlan(), now, planStartDate);
-        PhaseEvent nextPhaseEvent = PhaseEventData.getNextPhaseEvent(nextTimedPhase, subscription, now);
-        if (nextPhaseEvent != null) {
-            uncancelEvents.add(nextPhaseEvent);
+            DateTime planStartDate = subscription.getCurrentPlanStart();
+            TimedPhase nextTimedPhase = planAligner.getNextTimedPhase(subscription, subscription.getCurrentPlan(), now, planStartDate);
+            PhaseEvent nextPhaseEvent = PhaseEventData.getNextPhaseEvent(nextTimedPhase, subscription, now);
+            if (nextPhaseEvent != null) {
+                uncancelEvents.add(nextPhaseEvent);
+            }
+            dao.uncancelSubscription(subscription.getId(), uncancelEvents);
+            subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId()), catalogService.getCatalog());
+        } catch (CatalogApiException e) {
+            throw new EntitlementUserApiException(e);
         }
-        dao.uncancelSubscription(subscription.getId(), uncancelEvents);
-        subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId()), catalogService.getCatalog());
     }
 
     public void changePlan(SubscriptionData subscription, String productName, BillingPeriod term,
-            String priceList, DateTime requestedDate) throws EntitlementUserApiException {
+            String priceList, DateTime requestedDate)
+        throws EntitlementUserApiException {
 
+        try {
         requestedDate = (requestedDate != null) ? DefaultClock.truncateMs(requestedDate) : null;
         String currentPriceList = subscription.getCurrentPriceList();
 
@@ -184,12 +195,6 @@ public class SubscriptionApiService {
         try {
 
             IProduct destProduct = catalogService.getCatalog().findProduct(productName);
-            // STEPH really catalog exception
-            if (destProduct == null) {
-                throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_BAD_CATALOG,
-                        productName, term.toString(), "");
-            }
-
             IPlan currentPlan = subscription.getCurrentPlan();
             PlanPhaseSpecifier fromPlanPhase = new PlanPhaseSpecifier(currentPlan.getProduct().getName(),
                     currentPlan.getProduct().getCategory(),
@@ -208,20 +213,7 @@ public class SubscriptionApiService {
         ActionPolicy policy = planChangeResult.getPolicy();
         IPriceList newPriceList = planChangeResult.getNewPriceList();
 
-        //TODO: Correctly handle exception
-        IPlan newPlan = null;
-        try {
-            newPlan = catalogService.getCatalog().findPlan(productName, term, newPriceList.getName());
-        } catch (CatalogApiException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        if (newPlan == null) {
-            throw new EntitlementUserApiException(ErrorCode.ENT_CREATE_BAD_CATALOG,
-                    productName, term.toString(), newPriceList.getName());
-        }
-
+        IPlan newPlan = catalogService.getCatalog().findPlan(productName, term, newPriceList.getName());
         DateTime effectiveDate = subscription.getPlanChangeEffectiveDate(policy, now);
 
         TimedPhase currentTimedPhase = planAligner.getCurrentTimedPhaseOnChange(subscription, newPlan, newPriceList.getName(), effectiveDate);
@@ -246,9 +238,8 @@ public class SubscriptionApiService {
         changeEvents.add(changeEvent);
         dao.changePlan(subscription.getId(), changeEvents);
         subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId()), catalogService.getCatalog());
+        } catch (CatalogApiException e) {
+            throw new EntitlementUserApiException(e);
+        }
     }
-
-
-
-
 }
diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionData.java b/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionData.java
index 9f39dcb..71ac2f2 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionData.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/api/user/SubscriptionData.java
@@ -24,6 +24,8 @@ import java.util.List;
 import java.util.UUID;
 
 import org.joda.time.DateTime;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.ning.billing.ErrorCode;
 
@@ -45,6 +47,7 @@ import com.ning.billing.util.clock.Clock;
 
 public class SubscriptionData implements Subscription {
 
+    private final static Logger log = LoggerFactory.getLogger(SubscriptionData.class);
 
     private final Clock clock;
     private final SubscriptionApiService apiService;
@@ -379,32 +382,18 @@ public class SubscriptionData implements Subscription {
                         cur.getType()));
             }
 
-            //TODO: Correctly handle exceptions
             IPlan previousPlan = null;
             IPlanPhase previousPhase = null;
             IPlan nextPlan = null;
             IPlanPhase nextPhase = null;
             try {
-                previousPlan = catalog.findPlan(previousPlanName);
+                previousPlan = (previousPlanName != null) ? catalog.findPlan(previousPlanName) : null;
+                previousPhase = (previousPhase != null) ? catalog.findPhase(previousPhaseName) : null;
+                nextPlan = (nextPlanName != null) ? catalog.findPlan(nextPlanName) : null;
+                nextPhase = (nextPhaseName != null) ? catalog.findPhase(nextPhaseName) : null;
             } catch (CatalogApiException e) {
-                // TODO: handle exception
+                log.error(String.format("Failed to build transition for subscription %s", id), e);
             }
-            try {
-                previousPhase = catalog.findPhase(previousPhaseName);
-            } catch (CatalogApiException e) {
-                // TODO: handle exception
-            }
-            try {
-                nextPlan = catalog.findPlan(nextPlanName);
-            } catch (CatalogApiException e) {
-                // TODO: handle exception
-            }
-            try {
-                nextPhase = catalog.findPhase(nextPhaseName);
-            } catch (CatalogApiException e) {
-                // TODO: handle exception
-            }
-
             SubscriptionTransitionData transition =
                 new SubscriptionTransitionData(cur.getId(),
                         id,
diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java b/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
index 972e972..b2f3545 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
@@ -21,6 +21,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.inject.Inject;
+import com.ning.billing.catalog.api.CatalogApiException;
 import com.ning.billing.config.EntitlementConfig;
 
 import com.ning.billing.entitlement.alignment.PlanAligner;
@@ -44,6 +45,7 @@ import com.ning.billing.lifecycle.LifecycleHandlerType.LifecycleLevel;
 import com.ning.billing.util.clock.Clock;
 import com.ning.billing.util.eventbus.EventBus;
 import com.ning.billing.util.eventbus.EventBus.EventBusException;
+import com.sun.org.apache.xml.internal.resolver.CatalogException;
 
 public class Engine implements EventListener, EntitlementService {
 
@@ -167,7 +169,7 @@ public class Engine implements EventListener, EntitlementService {
                     (System.nanoTime() - ini) / NANO_TO_MS < MAX_NOTIFICATION_THREAD_WAIT_MS);
 
             if (!startedNotificationThread) {
-                log.error("Could not start notification thread in {} msec !!!", MAX_NOTIFICATION_THREAD_WAIT_MS);
+                log.error("Could not start notification thread in %d msec !!!", MAX_NOTIFICATION_THREAD_WAIT_MS);
                 throw new EntitlementError("Failed to start service!!");
             }
             log.info("Notification thread has been started in {} ms", (System.nanoTime() - ini) / NANO_TO_MS);
@@ -175,14 +177,15 @@ public class Engine implements EventListener, EntitlementService {
     }
 
     private void insertNextPhaseEvent(SubscriptionData subscription) {
-
-        DateTime now = clock.getUTCNow();
-
-        TimedPhase nextTimedPhase = planAligner.getNextTimedPhase(subscription, subscription.getCurrentPlan(), now, subscription.getCurrentPlanStart());
-        PhaseEvent nextPhaseEvent = PhaseEventData.getNextPhaseEvent(nextTimedPhase, subscription, now);
-        if (nextPhaseEvent != null) {
-            dao.createNextPhaseEvent(subscription.getId(), nextPhaseEvent);
+        try {
+            DateTime now = clock.getUTCNow();
+            TimedPhase nextTimedPhase = planAligner.getNextTimedPhase(subscription, subscription.getCurrentPlan(), now, subscription.getCurrentPlanStart());
+            PhaseEvent nextPhaseEvent = PhaseEventData.getNextPhaseEvent(nextTimedPhase, subscription, now);
+            if (nextPhaseEvent != null) {
+                dao.createNextPhaseEvent(subscription.getId(), nextPhaseEvent);
+            }
+        } catch (CatalogApiException e) {
+            log.error(String.format("Failed to insert next phase for subscription %s", subscription.getId()), e);
         }
     }
-
 }