Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion howfast_apm/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions tests/test_middleware_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -83,7 +87,7 @@ def test_with_exception(HowFastFlaskMiddleware):
middleware = HowFastFlaskMiddleware(app, app_id='some-dsn')

tester = app.test_client()
response = tester.get('/error')
response = tester.get('/exception')
assert response.status_code == 500
assert middleware._save_point.called is True
assert middleware._save_point.call_count == 1
Expand All @@ -92,6 +96,26 @@ def test_with_exception(HowFastFlaskMiddleware):
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()
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]
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') == "/error"


Expand Down