From 0eb347354b43cb21941a57b2faaef8bb6a583b62 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 1 Aug 2024 14:36:59 -0700 Subject: [PATCH 1/4] fix stack trace --- notifications_utils/request_helper.py | 28 ++++++++++++-------- tests/app/delivery/test_send_to_providers.py | 14 +++++++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/notifications_utils/request_helper.py b/notifications_utils/request_helper.py index 6c9edc8a7..35f51f42c 100644 --- a/notifications_utils/request_helper.py +++ b/notifications_utils/request_helper.py @@ -62,22 +62,28 @@ def __init__(self, app): self._app = app def __call__(self, environ, start_response): - req = NotifyRequest(environ) + try: + req = NotifyRequest(environ) - def rewrite_response_headers(status, headers, exc_info=None): - lower_existing_header_names = frozenset( - name.lower() for name, value in headers - ) + def rewrite_response_headers(status, headers, exc_info=None): + lower_existing_header_names = frozenset( + name.lower() for name, value in headers + ) - if TRACE_ID_HEADER.lower() not in lower_existing_header_names: - headers.append((TRACE_ID_HEADER, str(req.trace_id))) + if TRACE_ID_HEADER.lower() not in lower_existing_header_names: + headers.append((TRACE_ID_HEADER, str(req.trace_id))) - if SPAN_ID_HEADER.lower() not in lower_existing_header_names: - headers.append((SPAN_ID_HEADER, str(req.span_id))) + if SPAN_ID_HEADER.lower() not in lower_existing_header_names: + headers.append((SPAN_ID_HEADER, str(req.span_id))) - return start_response(status, headers, exc_info) + return start_response(status, headers, exc_info) - return self._app(environ, rewrite_response_headers) + return self._app(environ, rewrite_response_headers) + except BaseException as be: # noqa + if "AuthError" in str(be): + current_app.logger.error(be) + else: + raise be def init_app(app): diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 7f16a799a..63ab31ec7 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -330,7 +330,7 @@ def test_should_send_sms_with_downgraded_content(notify_db_session, mocker): template=template, ) db_notification.personalisation = {"misc": placeholder} - db_notification.reply_to_text = 'testing' + db_notification.reply_to_text = "testing" mocker.patch("app.aws_sns_client.send_sms") @@ -622,7 +622,7 @@ def test_should_update_billable_units_and_status_according_to_research_mode_and_ billable_units=0, status=NotificationStatus.CREATED, key_type=key_type, - reply_to_text='testing', + reply_to_text="testing", ) mocker.patch("app.aws_sns_client.send_sms") mocker.patch( @@ -786,7 +786,10 @@ def test_send_sms_to_provider_should_use_normalised_to(mocker, client, sample_te ) send_mock = mocker.patch("app.aws_sns_client.send_sms") notification = create_notification( - template=sample_template, to_field="+12028675309", normalised_to="2028675309", reply_to_text='testing' + template=sample_template, + to_field="+12028675309", + normalised_to="2028675309", + reply_to_text="testing", ) mock_s3 = mocker.patch("app.delivery.send_to_providers.get_phone_number_from_s3") @@ -862,7 +865,10 @@ def test_send_sms_to_provider_should_return_template_if_found_in_redis( send_mock = mocker.patch("app.aws_sns_client.send_sms") notification = create_notification( - template=sample_template, to_field="+447700900855", normalised_to="447700900855", reply_to_text='testing' + template=sample_template, + to_field="+447700900855", + normalised_to="447700900855", + reply_to_text="testing", ) mock_s3 = mocker.patch("app.delivery.send_to_providers.get_phone_number_from_s3") From dd05268cdbd95facf6b4622b75a64c9c1162371f Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Fri, 2 Aug 2024 08:06:06 -0700 Subject: [PATCH 2/4] fix notify-api-1394 as well --- notifications_utils/request_helper.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/notifications_utils/request_helper.py b/notifications_utils/request_helper.py index 35f51f42c..6d570fe06 100644 --- a/notifications_utils/request_helper.py +++ b/notifications_utils/request_helper.py @@ -82,6 +82,8 @@ def rewrite_response_headers(status, headers, exc_info=None): except BaseException as be: # noqa if "AuthError" in str(be): current_app.logger.error(be) + elif "AttributeError" in str(be): + current_app.logger.error(be) else: raise be From a3ddb35fe76252fd95dc8754a1e10b72c8a1d3fb Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Fri, 2 Aug 2024 08:06:41 -0700 Subject: [PATCH 3/4] comments --- notifications_utils/request_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notifications_utils/request_helper.py b/notifications_utils/request_helper.py index 6d570fe06..9ac35dfcf 100644 --- a/notifications_utils/request_helper.py +++ b/notifications_utils/request_helper.py @@ -80,9 +80,9 @@ def rewrite_response_headers(status, headers, exc_info=None): return self._app(environ, rewrite_response_headers) except BaseException as be: # noqa - if "AuthError" in str(be): + if "AuthError" in str(be): #notify-api-1135 current_app.logger.error(be) - elif "AttributeError" in str(be): + elif "AttributeError" in str(be): #notify-api-1394 current_app.logger.error(be) else: raise be From 2917c73be7ec735a4cbbd0693a6fab2949289abd Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Fri, 2 Aug 2024 08:33:45 -0700 Subject: [PATCH 4/4] fix flake 8 --- notifications_utils/request_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notifications_utils/request_helper.py b/notifications_utils/request_helper.py index 9ac35dfcf..eb7a2061f 100644 --- a/notifications_utils/request_helper.py +++ b/notifications_utils/request_helper.py @@ -80,9 +80,9 @@ def rewrite_response_headers(status, headers, exc_info=None): return self._app(environ, rewrite_response_headers) except BaseException as be: # noqa - if "AuthError" in str(be): #notify-api-1135 + if "AuthError" in str(be): # notify-api-1135 current_app.logger.error(be) - elif "AttributeError" in str(be): #notify-api-1394 + elif "AttributeError" in str(be): # notify-api-1394 current_app.logger.error(be) else: raise be