mirror of
https://github.com/GSA/notifications-api.git
synced 2026-04-20 17:29:54 -04:00
Add robustness to our v2 schema error messages:
* Ensure we dont raise exception if e.cause does not contain a message * Ensure we handle case where e.path may be empty * Refactor existing tests to conform to new format
This commit is contained in:
@@ -42,4 +42,25 @@ def build_error_message(errors):
|
||||
|
||||
|
||||
def __format_message(e):
|
||||
return e.message.replace("'", "") if not e.cause else "{} {}".format(e.path[0], e.cause.message)
|
||||
def get_path(e):
|
||||
error_path = None
|
||||
try:
|
||||
error_path = e.path[0]
|
||||
finally:
|
||||
return error_path
|
||||
|
||||
def get_error_message(e):
|
||||
error_message = None
|
||||
try:
|
||||
error_message = e.cause.message
|
||||
except AttributeError:
|
||||
error_message = e.message
|
||||
finally:
|
||||
return error_message.replace("'", '')
|
||||
|
||||
path = get_path(e)
|
||||
message = get_error_message(e)
|
||||
if path:
|
||||
return "{} {}".format(path, message)
|
||||
else:
|
||||
return "{}".format(message)
|
||||
|
||||
@@ -109,7 +109,6 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist
|
||||
assert '<!DOCTYPE html' in app.aws_ses_client.send_email.call_args[1]['html_body']
|
||||
|
||||
notification = Notification.query.filter_by(id=db_notification.id).one()
|
||||
|
||||
assert notification.status == 'sending'
|
||||
assert notification.sent_at <= datetime.utcnow()
|
||||
assert notification.sent_by == 'ses'
|
||||
|
||||
@@ -218,7 +218,7 @@ def test_get_all_notifications_filter_by_template_type_invalid_template_type(cli
|
||||
|
||||
assert json_response['status_code'] == 400
|
||||
assert len(json_response['errors']) == 1
|
||||
assert json_response['errors'][0]['message'] == "orange is not one of [sms, email, letter]"
|
||||
assert json_response['errors'][0]['message'] == "template_type orange is not one of [sms, email, letter]"
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_single_status(client, notify_db, notify_db_session):
|
||||
@@ -255,7 +255,7 @@ def test_get_all_notifications_filter_by_status_invalid_status(client, sample_no
|
||||
|
||||
assert json_response['status_code'] == 400
|
||||
assert len(json_response['errors']) == 1
|
||||
assert json_response['errors'][0]['message'] == "elephant is not one of [created, sending, delivered, " \
|
||||
assert json_response['errors'][0]['message'] == "status elephant is not one of [created, sending, delivered, " \
|
||||
"pending, failed, technical-failure, temporary-failure, permanent-failure]"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user