Fix always error message for cloud notifications

This commit is contained in:
Innokentii Konstantinov 2022-06-09 15:48:43 +04:00
parent 94af30b8bb
commit 3fae0c9ff2
3 changed files with 12 additions and 5 deletions

View file

@ -32,6 +32,7 @@ class MakeCallView(APIView):
response_data = {}
organization = self.request.auth.organization
logger.info(f"Making cloud call. Email {serializer.validated_data['email']}")
user = organization.users.filter(
email=serializer.validated_data["email"], _verified_phone_number__isnull=False
).first()
@ -41,9 +42,11 @@ class MakeCallView(APIView):
try:
PhoneCall.make_grafana_cloud_call(user, serializer.validated_data["message"])
except TwilioRestException:
except TwilioRestException as e:
logger.info(f"Making cloud call. Twilio exception {str(e)}")
return Response(status=status.HTTP_503_SERVICE_UNAVAILABLE, data=response_data)
except PhoneCall.PhoneCallsLimitExceeded:
logger.info(f"Making cloud call. PhoneCallsLimitExceeded")
return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "limit-exceeded"})
return Response(status=status.HTTP_200_OK, data=response_data)
@ -73,9 +76,11 @@ class SendSMSView(APIView):
try:
SMSMessage.send_grafana_cloud_sms(user, serializer.validated_data["message"])
except TwilioRestException:
except TwilioRestException as e:
logger.info(f"Sending cloud sms. Twilio exception {str(e)}")
return Response(status=status.HTTP_503_SERVICE_UNAVAILABLE, data=response_data)
except SMSMessage.SMSLimitExceeded:
logger.info(f"Sending cloud sms. PhoneCallsLimitExceeded")
return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "limit-exceeded"})
return Response(status=status.HTTP_200_OK, data=response_data)

View file

@ -169,7 +169,8 @@ class PhoneCall(models.Model):
except requests.exceptions.RequestException as e:
logger.warning(f"Unable to make call through cloud. Request exception {str(e)}")
raise PhoneCall.CloudSendError("Unable to make call through cloud: request failed")
if response.status_code == status.HTTP_200_OK:
logger.info("Make cloud call successfully")
if response.status_code == status.HTTP_400_BAD_REQUEST and response.json().get("error") == "limit-exceeded":
raise PhoneCall.PhoneCallsLimitExceeded("Organization calls limit exceeded")
elif response.status_code == status.HTTP_404_NOT_FOUND:

View file

@ -134,8 +134,9 @@ class SMSMessage(models.Model):
except requests.exceptions.RequestException as e:
logger.warning(f"Unable to send SMS through cloud. Request exception {str(e)}")
raise SMSMessage.CloudSendError("Unable to send SMS through cloud: request failed")
if response.status_code == status.HTTP_400_BAD_REQUEST and response.json().get("error") == "limit-exceeded":
if response.status_code == status.HTTP_200_OK:
logger.info("Sent cloud sms successfully")
elif response.status_code == status.HTTP_400_BAD_REQUEST and response.json().get("error") == "limit-exceeded":
raise SMSMessage.SMSLimitExceeded("Organization sms limit exceeded")
elif response.status_code == status.HTTP_404_NOT_FOUND:
raise SMSMessage.CloudSendError("Unable to send SMS through cloud: user not found")