From 3ca70bef777c80e9b2c1152f89565c807d7cc705 Mon Sep 17 00:00:00 2001 From: Evan Date: Mon, 23 Oct 2023 10:35:56 +0000 Subject: [PATCH] Use custom error class for email change failures --- judge/views/user.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/judge/views/user.py b/judge/views/user.py index 7d006169d8..8fe6e5b17c 100644 --- a/judge/views/user.py +++ b/judge/views/user.py @@ -576,6 +576,9 @@ def post(self, request, *args, **kwargs): class EmailChangeActivateView(LoginRequiredMixin, View): + class EmailChangeFailedError(Exception): + pass + def get(self, request, *args, **kwargs): activation_key = kwargs['activation_key'] signer = signing.TimestampSigner() @@ -586,22 +589,24 @@ def get(self, request, *args, **kwargs): max_age=settings.DMOJ_EMAIL_CHANGE_EXPIRY_MINUTES * 60, ) except (binascii.Error, signing.BadSignature): - raise ValueError(_('Invalid activation key. Please try again.')) + raise self.EmailChangeFailedError(_('Invalid activation key. Please try again.')) except signing.SignatureExpired: - raise ValueError(_('This request has expired. Please try again.')) + raise self.EmailChangeFailedError(_('This request has expired. Please try again.')) if data['id'] != request.user.id: - raise ValueError( + raise self.EmailChangeFailedError( _('Please try again from the account this email change was originally requested from.'), ) with revisions.create_revision(atomic=True): if User.objects.filter(email=data['email']).exists(): - raise ValueError(_('The email originally requested is in use. Please try again with a new email.')) + raise EmailChangeFailedError( + _('The email originally requested is in use. Please try again with a new email.'), + ) request.user.email = data['email'] request.user.save() revisions.set_user(request.user) revisions.set_comment(_('Changed email address')) - except ValueError as ve: - return generic_message(request, _('Email change failed'), str(ve), status=403) + except self.EmailChangeFailedError as e: + return generic_message(request, _('Email change failed'), str(e), status=403) else: return generic_message( request,