Improve error handling

This commit is contained in:
Shane Kilkelly 2016-08-16 15:19:36 +01:00
parent cb4f6391a2
commit 81d0edf716

View file

@ -22,6 +22,7 @@ module.exports = SubscriptionController =
viewName = "#{viewName}_#{req.query.v}" viewName = "#{viewName}_#{req.query.v}"
logger.log viewName:viewName, "showing plans page" logger.log viewName:viewName, "showing plans page"
GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency)-> GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency)->
return next(err) if err?
res.render viewName, res.render viewName,
title: "plans_and_pricing" title: "plans_and_pricing"
plans: plans plans: plans
@ -71,12 +72,13 @@ module.exports = SubscriptionController =
AuthenticationController.getLoggedInUser req, (error, user) => AuthenticationController.getLoggedInUser req, (error, user) =>
return next(error) if error? return next(error) if error?
LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)-> LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
return next(err) if err?
groupLicenceInviteUrl = SubscriptionDomainHandler.getDomainLicencePage(user) groupLicenceInviteUrl = SubscriptionDomainHandler.getDomainLicencePage(user)
if subscription?.customAccount if subscription?.customAccount
logger.log user: user, "redirecting to custom account page" logger.log user: user, "redirecting to custom account page"
res.redirect "/user/subscription/custom_account" res.redirect "/user/subscription/custom_account"
else if groupLicenceInviteUrl? and !hasSubOrIsGroupMember else if groupLicenceInviteUrl? and !hasSubOrIsGroupMember
logger.log user:user, "redirecting to group subscription invite page" logger.log user:user, "redirecting to group subscription invite page"
res.redirect groupLicenceInviteUrl res.redirect groupLicenceInviteUrl
else if !hasSubOrIsGroupMember else if !hasSubOrIsGroupMember
logger.log user: user, "redirecting to plans" logger.log user: user, "redirecting to plans"
@ -99,7 +101,9 @@ module.exports = SubscriptionController =
userCustomSubscriptionPage: (req, res, next)-> userCustomSubscriptionPage: (req, res, next)->
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
return next(error) if error?
LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)-> LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
return next(err) if err?
if !subscription? if !subscription?
err = new Error("subscription null for custom account, user:#{user?._id}") err = new Error("subscription null for custom account, user:#{user?._id}")
logger.warn err:err, "subscription is null for custom accounts page" logger.warn err:err, "subscription is null for custom accounts page"
@ -113,6 +117,7 @@ module.exports = SubscriptionController =
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
return next(error) if error? return next(error) if error?
LimitationsManager.userHasSubscription user, (err, hasSubscription)-> LimitationsManager.userHasSubscription user, (err, hasSubscription)->
return next(err) if err?
if !hasSubscription if !hasSubscription
res.redirect "/user/subscription" res.redirect "/user/subscription"
else else
@ -142,23 +147,26 @@ module.exports = SubscriptionController =
return res.sendStatus 500 return res.sendStatus 500
res.sendStatus 201 res.sendStatus 201
successful_subscription: (req, res)-> successful_subscription: (req, res, next)->
AuthenticationController.getLoggedInUser req, (error, user) => AuthenticationController.getLoggedInUser req, (error, user) =>
return next(error) if error?
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription) -> SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription) ->
return next(error) if error?
res.render "subscriptions/successful_subscription", res.render "subscriptions/successful_subscription",
title: "thank_you" title: "thank_you"
subscription:subscription subscription:subscription
cancelSubscription: (req, res, next) -> cancelSubscription: (req, res, next) ->
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
logger.log user_id:user._id, "canceling subscription"
return next(error) if error? return next(error) if error?
logger.log user_id:user._id, "canceling subscription"
SubscriptionHandler.cancelSubscription user, (err)-> SubscriptionHandler.cancelSubscription user, (err)->
if err? if err?
logger.err err:err, user_id:user._id, "something went wrong canceling subscription" logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
return next(err)
res.redirect "/user/subscription" res.redirect "/user/subscription"
updateSubscription: (req, res)-> updateSubscription: (req, res, next)->
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
return next(error) if error? return next(error) if error?
planCode = req.body.plan_code planCode = req.body.plan_code
@ -166,30 +174,35 @@ module.exports = SubscriptionController =
SubscriptionHandler.updateSubscription user, planCode, null, (err)-> SubscriptionHandler.updateSubscription user, planCode, null, (err)->
if err? if err?
logger.err err:err, user_id:user._id, "something went wrong updating subscription" logger.err err:err, user_id:user._id, "something went wrong updating subscription"
return next(err)
res.redirect "/user/subscription" res.redirect "/user/subscription"
reactivateSubscription: (req, res)-> reactivateSubscription: (req, res, next)->
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
logger.log user_id:user._id, "reactivating subscription"
return next(error) if error? return next(error) if error?
logger.log user_id:user._id, "reactivating subscription"
SubscriptionHandler.reactivateSubscription user, (err)-> SubscriptionHandler.reactivateSubscription user, (err)->
if err? if err?
logger.err err:err, user_id:user._id, "something went wrong reactivating subscription" logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
return next(err)
res.redirect "/user/subscription" res.redirect "/user/subscription"
recurlyCallback: (req, res)-> recurlyCallback: (req, res, next)->
logger.log data: req.body, "received recurly callback" logger.log data: req.body, "received recurly callback"
# we only care if a subscription has exipired # we only care if a subscription has exipired
if req.body? and req.body["expired_subscription_notification"]? if req.body? and req.body["expired_subscription_notification"]?
recurlySubscription = req.body["expired_subscription_notification"].subscription recurlySubscription = req.body["expired_subscription_notification"].subscription
SubscriptionHandler.recurlyCallback recurlySubscription, -> SubscriptionHandler.recurlyCallback recurlySubscription, (err)->
return next(err) if err?
res.sendStatus 200 res.sendStatus 200
else else
res.sendStatus 200 res.sendStatus 200
renderUpgradeToAnnualPlanPage: (req, res)-> renderUpgradeToAnnualPlanPage: (req, res, next)->
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
return next(error) if error?
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)-> LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
return next(err) if err?
planCode = subscription?.planCode.toLowerCase() planCode = subscription?.planCode.toLowerCase()
if planCode?.indexOf("annual") != -1 if planCode?.indexOf("annual") != -1
planName = "annual" planName = "annual"
@ -204,8 +217,9 @@ module.exports = SubscriptionController =
title: "Upgrade to annual" title: "Upgrade to annual"
planName: planName planName: planName
processUpgradeToAnnualPlan: (req, res)-> processUpgradeToAnnualPlan: (req, res, next)->
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
return next(error) if error?
{planName} = req.body {planName} = req.body
coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName] coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
annualPlanName = "#{planName}-annual" annualPlanName = "#{planName}-annual"
@ -213,13 +227,14 @@ module.exports = SubscriptionController =
SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)-> SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
if err? if err?
logger.err err:err, user_id:user._id, "error updating subscription" logger.err err:err, user_id:user._id, "error updating subscription"
res.sendStatus 500 return next(err)
else res.sendStatus 200
res.sendStatus 200
extendTrial: (req, res)-> extendTrial: (req, res, next)->
AuthenticationController.getLoggedInUser req, (error, user) -> AuthenticationController.getLoggedInUser req, (error, user) ->
return next(error) if error?
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)-> LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
return next(err) if err?
SubscriptionHandler.extendTrial subscription, 14, (err)-> SubscriptionHandler.extendTrial subscription, 14, (err)->
if err? if err?
res.send 500 res.send 500