Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BaseFailed exceptions for phone_notificator #2074

Merged
merged 8 commits into from
Jun 8, 2023
32 changes: 18 additions & 14 deletions engine/apps/api/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from apps.mobile_app.demo_push import send_test_push
from apps.mobile_app.exceptions import DeviceNotSet
from apps.phone_notifications.exceptions import (
BaseFailed,
FailedToFinishVerification,
FailedToMakeCall,
FailedToStartVerification,
Expand Down Expand Up @@ -340,8 +341,8 @@ def get_verification_code(self, request, pk):
phone_backend.send_verification_sms(user)
except NumberAlreadyVerified:
return Response("Phone number already verified", status=status.HTTP_400_BAD_REQUEST)
except FailedToStartVerification:
return Response("Something went wrong while sending code", status=status.HTTP_500_INTERNAL_SERVER_ERROR)
except FailedToStartVerification as e:
return handle_phone_notificator_failed(e)
except ProviderNotSupports:
return Response(
"Phone provider not supports sms verification", status=status.HTTP_500_INTERNAL_SERVER_ERROR
Expand All @@ -367,8 +368,8 @@ def get_verification_call(self, request, pk):
phone_backend.make_verification_call(user)
except NumberAlreadyVerified:
return Response("Phone number already verified", status=status.HTTP_400_BAD_REQUEST)
except FailedToStartVerification:
return Response("Something went wrong while calling", status=status.HTTP_500_INTERNAL_SERVER_ERROR)
except FailedToStartVerification as e:
return handle_phone_notificator_failed(e)
except ProviderNotSupports:
return Response(
"Phone provider not supports call verification", status=status.HTTP_500_INTERNAL_SERVER_ERROR
Expand All @@ -390,8 +391,8 @@ def verify_number(self, request, pk):
phone_backend = PhoneBackend()
try:
verified = phone_backend.verify_phone_number(target_user, code)
except FailedToFinishVerification:
return Response("Something went wrong while verifying code", status=status.HTTP_503_SERVICE_UNAVAILABLE)
except FailedToFinishVerification as e:
return handle_phone_notificator_failed(e)
if verified:
new_state = target_user.insight_logs_serialized
write_resource_insight_log(
Expand Down Expand Up @@ -432,10 +433,8 @@ def make_test_call(self, request, pk):
phone_backend.make_test_call(user)
except NumberNotVerified:
return Response("Phone number is not verified", status=status.HTTP_400_BAD_REQUEST)
except FailedToMakeCall:
return Response(
"Something went wrong while making a test call", status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
except FailedToMakeCall as e:
return handle_phone_notificator_failed(e)
except ProviderNotSupports:
return Response("Phone provider not supports phone calls", status=status.HTTP_500_INTERNAL_SERVER_ERROR)

Expand All @@ -449,10 +448,8 @@ def send_test_sms(self, request, pk):
phone_backend.send_test_sms(user)
except NumberNotVerified:
return Response("Phone number is not verified", status=status.HTTP_400_BAD_REQUEST)
except FailedToMakeCall:
return Response(
"Something went wrong while making a test call", status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
except FailedToMakeCall as e:
return handle_phone_notificator_failed(e)
except ProviderNotSupports:
return Response("Phone provider not supports phone calls", status=status.HTTP_500_INTERNAL_SERVER_ERROR)

Expand Down Expand Up @@ -650,3 +647,10 @@ def check_availability(self, request, pk):
user = self.get_object()
warnings = check_user_availability(user=user, team=request.user.current_team)
return Response(data={"warnings": warnings}, status=status.HTTP_200_OK)


def handle_phone_notificator_failed(exc: BaseFailed) -> Response:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def handle_phone_notificator_failed(exc: BaseFailed) -> Response:
def handle_phone_notification_failed(exc: BaseFailed) -> Response:

if exc.graceful_msg:
return Response(exc.graceful_msg, status=status.HTTP_400_BAD_REQUEST)
else:
return Response("Something went wrong", status=status.HTTP_503_SERVICE_UNAVAILABLE)
28 changes: 22 additions & 6 deletions engine/apps/phone_notifications/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
class FailedToMakeCall(Exception):
class BaseFailed(Exception):
"""
Failed is base exception for all Failed... exceptions.
This exception is indicates error while performing some phone notification operation.
Optionally can contain graceful_msg attribute. When graceful_msg is provided it mean that error on provider side is
not our fault, but some provider error (number is blocked, fraud guard, ...).
By default, graceful_msg is None - it means that error is our fault (network problems, invalid configuration,...).

Attributes:
graceful_msg: string with some details about exception which can be exposed to caller.
"""

def __init__(self, graceful_msg=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is graceful_msg being set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nowhere yet. but it's expected to be set in concrete providers, me or @mderynck will add this

self.graceful_msg = graceful_msg


class FailedToMakeCall(BaseFailed):
pass


class FailedToSendSMS(Exception):
class FailedToSendSMS(BaseFailed):
pass


class NumberNotVerified(Exception):
class FailedToStartVerification(BaseFailed):
pass


class NumberAlreadyVerified(Exception):
class FailedToFinishVerification(BaseFailed):
pass


class FailedToStartVerification(Exception):
class NumberNotVerified(Exception):
pass


class FailedToFinishVerification(Exception):
class NumberAlreadyVerified(Exception):
pass


Expand Down