mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
Raise better exception on InvalidParameterValue error
There are several reasons why we might get an `InvalidParameterValue` from the SES API. One, as correctly identified before in https://github.com/alphagov/notifications-api/pull/713/files is if we allow an email address on our side that SES rejects. However, there are other types of errors that could cause an `InvalidParameterValue`. One example is a `Header too long: 'Subject'` error that we have seen happen in production. This shouldn't raise an `InvalidEmailError` as that is not appropriate. Therefore, we introduce a new exception `EmailClientNonRetryableException`, that represents any exception back from an email client that we can use whenever we get a `InvalidParameterValue` error. Note, I chose `EmailClientNonRetryableException` rather than `SESClientNonRetryableException` as our code needs to catch this exception and it shouldn't be aware of what email client is being used, it just needs to know that it came from one of the email clients (if in time we have more than one). In time, we may wish to extend the approach of having generic `EmailClient` exceptions and `SMSClient` exceptions as this should be the most extendable pattern and a good abstraction.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import botocore
|
||||
import pytest
|
||||
from unittest.mock import Mock, ANY
|
||||
from notifications_utils.recipients import InvalidEmailError
|
||||
|
||||
from app import aws_ses_client
|
||||
from app.clients.email import EmailClientNonRetryableException
|
||||
from app.clients.email.aws_ses import get_aws_responses, AwsSesClientException, AwsSesClientThrottlingSendRateException
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ def test_send_email_handles_punycode_to_address(notify_api, mocker):
|
||||
)
|
||||
|
||||
|
||||
def test_send_email_raises_bad_email_as_InvalidEmailError(mocker):
|
||||
def test_send_email_raises_invalid_parameter_value_error_as_EmailClientNonRetryableException(mocker):
|
||||
boto_mock = mocker.patch.object(aws_ses_client, '_client', create=True)
|
||||
mocker.patch.object(aws_ses_client, 'statsd_client', create=True)
|
||||
error_response = {
|
||||
@@ -104,7 +104,7 @@ def test_send_email_raises_bad_email_as_InvalidEmailError(mocker):
|
||||
boto_mock.send_email.side_effect = botocore.exceptions.ClientError(error_response, 'opname')
|
||||
mocker.patch.object(aws_ses_client, 'statsd_client', create=True)
|
||||
|
||||
with pytest.raises(InvalidEmailError) as excinfo:
|
||||
with pytest.raises(EmailClientNonRetryableException) as excinfo:
|
||||
aws_ses_client.send_email(
|
||||
source=Mock(),
|
||||
to_addresses='definitely@invalid_email.com',
|
||||
|
||||
Reference in New Issue
Block a user