From 1e79b6eaa97140969db6ebe0d27e731af25d270d Mon Sep 17 00:00:00 2001 From: Daniel Duke Date: Wed, 31 Mar 2021 14:35:55 -0700 Subject: [PATCH 1/5] add breaking test --- tests/integration/pyramid_tests.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/pyramid_tests.py b/tests/integration/pyramid_tests.py index 3be424002..e4c03e1e5 100644 --- a/tests/integration/pyramid_tests.py +++ b/tests/integration/pyramid_tests.py @@ -19,6 +19,7 @@ from baseplate.frameworks.pyramid import BaseplateConfigurator, ServerSpanInitialized from pyramid.config import Configurator + from pyramid.httpexceptions import HTTPNotImplemented except ImportError: raise unittest.SkipTest("pyramid/webtest is not installed") @@ -65,7 +66,7 @@ def make_iter(): def render_exception_view(request): - return + return HTTPNotImplemented(title="a fancy title", explanation="a fancy explanation") def render_bad_exception_view(request): @@ -230,7 +231,7 @@ def test_exception_caught(self): self.assertIsInstance(captured_exc, TestException) def test_control_flow_exception_not_caught(self): - self.test_app.get("/example?control_flow_exception") + response = self.test_app.get("/example?control_flow_exception", status=501) self.assertTrue(self.server_observer.on_start.called) self.assertTrue(self.server_observer.on_finish.called) @@ -245,6 +246,7 @@ def test_exception_in_exception_view_caught(self): self.assertTrue(self.server_observer.on_start.called) self.assertTrue(self.server_observer.on_finish.called) self.assertTrue(self.context_init_event_subscriber.called) + self.assertIn(b"a fancy explanation", response.body) _, captured_exc, _ = self.server_observer.on_finish.call_args[0][0] self.assertIsInstance(captured_exc, ExceptionViewException) From 93c88c30ac927393ada82614060b973d906f4777 Mon Sep 17 00:00:00 2001 From: Daniel Duke Date: Wed, 31 Mar 2021 14:39:41 -0700 Subject: [PATCH 2/5] add fix --- baseplate/frameworks/pyramid/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/baseplate/frameworks/pyramid/__init__.py b/baseplate/frameworks/pyramid/__init__.py index f71f08198..8f8a7417e 100644 --- a/baseplate/frameworks/pyramid/__init__.py +++ b/baseplate/frameworks/pyramid/__init__.py @@ -2,6 +2,7 @@ import logging import sys +from itertools import chain from typing import Any from typing import Callable from typing import Dict @@ -83,7 +84,16 @@ def baseplate_tween(request: Request) -> Response: else: if request.trace: request.trace.set_tag("http.status_code", response.status_code) - response.app_iter = SpanFinishingAppIterWrapper(request.trace, response.app_iter) + if hasattr(response, "explanation") and response.explanation: + response.app_iter = SpanFinishingAppIterWrapper( + request.trace, + chain( + response.app_iter, + [str.encode(response._status), str.encode(response.explanation)], + ), + ) + else: + response.app_iter = SpanFinishingAppIterWrapper(request.trace, response.app_iter) finally: # avoid a reference cycle request.start_server_span = None From 31bb22891576b91bca5c89e0288edd3e3354585c Mon Sep 17 00:00:00 2001 From: Daniel Duke Date: Wed, 31 Mar 2021 14:43:39 -0700 Subject: [PATCH 3/5] lint --- baseplate/frameworks/pyramid/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/baseplate/frameworks/pyramid/__init__.py b/baseplate/frameworks/pyramid/__init__.py index 8f8a7417e..fa6593b50 100644 --- a/baseplate/frameworks/pyramid/__init__.py +++ b/baseplate/frameworks/pyramid/__init__.py @@ -93,7 +93,9 @@ def baseplate_tween(request: Request) -> Response: ), ) else: - response.app_iter = SpanFinishingAppIterWrapper(request.trace, response.app_iter) + response.app_iter = SpanFinishingAppIterWrapper( + request.trace, response.app_iter + ) finally: # avoid a reference cycle request.start_server_span = None From 79384d5175e5ec5862d0d7e9c8eeac9c6462a3b0 Mon Sep 17 00:00:00 2001 From: Daniel Duke Date: Wed, 31 Mar 2021 14:45:33 -0700 Subject: [PATCH 4/5] Update pyramid_tests.py --- tests/integration/pyramid_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/pyramid_tests.py b/tests/integration/pyramid_tests.py index e4c03e1e5..0c729df2b 100644 --- a/tests/integration/pyramid_tests.py +++ b/tests/integration/pyramid_tests.py @@ -236,6 +236,7 @@ def test_control_flow_exception_not_caught(self): self.assertTrue(self.server_observer.on_start.called) self.assertTrue(self.server_observer.on_finish.called) self.assertTrue(self.context_init_event_subscriber.called) + self.assertIn(b"a fancy explanation", response.body) args, _ = self.server_observer.on_finish.call_args self.assertEqual(args[0], None) @@ -246,7 +247,6 @@ def test_exception_in_exception_view_caught(self): self.assertTrue(self.server_observer.on_start.called) self.assertTrue(self.server_observer.on_finish.called) self.assertTrue(self.context_init_event_subscriber.called) - self.assertIn(b"a fancy explanation", response.body) _, captured_exc, _ = self.server_observer.on_finish.call_args[0][0] self.assertIsInstance(captured_exc, ExceptionViewException) From f49734d2830e2f8bccdd8b087d256c568b0eb08b Mon Sep 17 00:00:00 2001 From: Daniel Duke Date: Thu, 1 Apr 2021 08:58:10 -0700 Subject: [PATCH 5/5] prints to test --- baseplate/frameworks/pyramid/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/baseplate/frameworks/pyramid/__init__.py b/baseplate/frameworks/pyramid/__init__.py index fa6593b50..651ab3b41 100644 --- a/baseplate/frameworks/pyramid/__init__.py +++ b/baseplate/frameworks/pyramid/__init__.py @@ -82,9 +82,11 @@ def baseplate_tween(request: Request) -> Response: request.trace.finish(exc_info=sys.exc_info()) raise else: + print("\n\n\n\nhere") if request.trace: request.trace.set_tag("http.status_code", response.status_code) if hasattr(response, "explanation") and response.explanation: + print("\n\n\n\nhere2") response.app_iter = SpanFinishingAppIterWrapper( request.trace, chain(