From 8c6624b0ebb24bb9b28578cec988c9d025d7cf5d Mon Sep 17 00:00:00 2001 From: Jean-Philippe CARUANA Date: Tue, 14 Apr 2020 16:32:58 +0200 Subject: [PATCH 1/4] reproduce UnboundLocalError: local variable 'end' referenced before assignment --- tests/test_middleware_flask.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/test_middleware_flask.py b/tests/test_middleware_flask.py index 5cfc699..e40b961 100644 --- a/tests/test_middleware_flask.py +++ b/tests/test_middleware_flask.py @@ -22,9 +22,13 @@ def external_call(): requests.put('https://does-not-exist/') return 'ok' + @app.route('/exception') + def exception(): + raise Exception("Unhandled exception, kaboom!") + @app.route('/error') def error(): - raise Exception("Unhandled exception, kaboom!") + raise SystemExit() return app @@ -82,6 +86,23 @@ def test_with_exception(HowFastFlaskMiddleware): app = create_app() middleware = HowFastFlaskMiddleware(app, app_id='some-dsn') + tester = app.test_client() + response = tester.get('/exception') + assert response.status_code == 500 + assert middleware._save_point.called is True + assert middleware._save_point.call_count == 1 + point = middleware._save_point.call_args[1] + assert point.get('time_elapsed') > 0 + assert point.get('time_request_started') < datetime.now(timezone.utc) + assert point.get('method') == "GET" + assert point.get('response_status') == "500 INTERNAL SERVER ERROR" + assert point.get('uri') == "/exception" + +def test_with_error(HowFastFlaskMiddleware): + """ The middleware should gracefully handle routes that raise an Error """ + app = create_app() + middleware = HowFastFlaskMiddleware(app, app_id='some-dsn') + tester = app.test_client() response = tester.get('/error') assert response.status_code == 500 From 1ef405e749c8c6fac8a36a6e0602bc348d376744 Mon Sep 17 00:00:00 2001 From: Jean-Philippe CARUANA Date: Tue, 14 Apr 2020 16:42:43 +0200 Subject: [PATCH 2/4] this fix does ont work :( --- howfast_apm/flask.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/howfast_apm/flask.py b/howfast_apm/flask.py index 87de3a3..f06e167 100644 --- a/howfast_apm/flask.py +++ b/howfast_apm/flask.py @@ -81,7 +81,7 @@ def _start_response_wrapped(status, *args, **kwargs): return_value = self.wsgi_app(environ, _start_response_wrapped) # Stop the timer as soon as possible to get the best measure of the function's execution time end = timer() - except Exception: + except BaseException: # The WSGI app raised an exception, let's still save the point before raising the # exception again # First, "stop" the timer now to get the good measure of the function's execution time From ec6c7d115607dc1e9f7fe89f0a8454fe82b3187a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Bergem?= Date: Thu, 16 Apr 2020 15:41:57 +0200 Subject: [PATCH 3/4] Fix test: Flask won't catch a SystemExit --- tests/test_middleware_flask.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_middleware_flask.py b/tests/test_middleware_flask.py index e40b961..fdb8e27 100644 --- a/tests/test_middleware_flask.py +++ b/tests/test_middleware_flask.py @@ -104,8 +104,10 @@ def test_with_error(HowFastFlaskMiddleware): middleware = HowFastFlaskMiddleware(app, app_id='some-dsn') tester = app.test_client() - response = tester.get('/error') - assert response.status_code == 500 + with pytest.raises(SystemExit): + # Flask will propagate the SystemExit instead of catching it + tester.get('/error') + # However, the failure should still be logged by the middleware assert middleware._save_point.called is True assert middleware._save_point.call_count == 1 point = middleware._save_point.call_args[1] From 16e1b77c965d59a3feec0c9805c73874872789c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Bergem?= Date: Thu, 16 Apr 2020 15:45:36 +0200 Subject: [PATCH 4/4] I need to install some format-on-save feature in vim --- tests/test_middleware_flask.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_middleware_flask.py b/tests/test_middleware_flask.py index fdb8e27..0a9ff82 100644 --- a/tests/test_middleware_flask.py +++ b/tests/test_middleware_flask.py @@ -98,6 +98,7 @@ def test_with_exception(HowFastFlaskMiddleware): assert point.get('response_status') == "500 INTERNAL SERVER ERROR" assert point.get('uri') == "/exception" + def test_with_error(HowFastFlaskMiddleware): """ The middleware should gracefully handle routes that raise an Error """ app = create_app()