From 1c485c188416e80172f75a6f5b31c9cbb7ee3c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Fern=C3=A1ndez=20Capel?= Date: Thu, 31 May 2018 16:42:09 +0100 Subject: [PATCH] Improve error handling --- .../Subscription/TeamInvitesController.coffee | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/services/web/app/coffee/Features/Subscription/TeamInvitesController.coffee b/services/web/app/coffee/Features/Subscription/TeamInvitesController.coffee index 7e44e319db..ef1d90aa6a 100644 --- a/services/web/app/coffee/Features/Subscription/TeamInvitesController.coffee +++ b/services/web/app/coffee/Features/Subscription/TeamInvitesController.coffee @@ -11,7 +11,7 @@ module.exports = email = req.body.email TeamInvitesHandler.createInvite teamManagerId, email, (err, invite) -> - next(err) if err? + return handleError(err, req, res, next) if err? inviteView = { user: { email: invite.email, sentAt: invite.sentAt, holdingAccount: true } } @@ -22,13 +22,13 @@ module.exports = userId = AuthenticationController.getLoggedInUserId(req) TeamInvitesHandler.getInvite token, (err, invite, teamSubscription) -> - next(err) if err? + return handleError(err, req, res, next) if err? - unless invite? + if !invite return ErrorController.notFound(req, res, next) SubscriptionLocator.getUsersSubscription userId, (err, personalSubscription) -> - return callback(err) if err? + return handleError(err, req, res, next) if err? res.render "subscriptions/team/invite", inviterName: invite.inviterName @@ -36,13 +36,12 @@ module.exports = hasPersonalSubscription: personalSubscription? appName: settings.appName - acceptInvite: (req, res, next) -> token = req.params.token userId = AuthenticationController.getLoggedInUserId(req) TeamInvitesHandler.acceptInvite token, userId, (err, results) -> - next(err) if err? + return handleError(err, req, res, next) if err? res.sendStatus 204 @@ -51,6 +50,12 @@ module.exports = teamManagerId = AuthenticationController.getLoggedInUserId(req) TeamInvitesHandler.revokeInvite teamManagerId, email, (err, results) -> - next(err) if err? + return handleError(err, req, res, next) if err? res.sendStatus 204 + +handleError = (err, req, res, next) -> + if err.teamNotFound or err.inviteNoLongerValid + ErrorController.notFound(req, res, next) + else + next(err)