Skip to content

Commit

Permalink
Merge pull request #390 from rollbar/bxsx/fix-starlette-log-error
Browse files Browse the repository at this point in the history
Fix #387: recursive error logging for non-http events
  • Loading branch information
bxsx authored Sep 21, 2021
2 parents faeacd6 + 427cab6 commit 166b601
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 4 additions & 8 deletions rollbar/contrib/starlette/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ def get_current_request() -> Optional[Request]:
)
return None

request = _current_request.get()

if request is None:
log.error('Request is not available in the present context.')
return None

return request
return _current_request.get()


def store_current_request(
Expand All @@ -63,8 +57,10 @@ def store_current_request(

if receive is None:
request = request_or_scope
else:
elif request_or_scope['type'] == 'http':
request = Request(request_or_scope, receive)
else:
request = None

_current_request.set(request)
return request
Expand Down
12 changes: 11 additions & 1 deletion rollbar/test/starlette_tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_should_accept_request_param(self):

self.assertEqual(request, stored_request)

def test_should_accept_scope_and_receive_params(self):
def test_should_accept_scope_param_if_http_type(self):
from starlette.requests import Request
from rollbar.contrib.starlette.requests import store_current_request
from rollbar.lib._async import async_receive
Expand Down Expand Up @@ -83,6 +83,16 @@ def test_should_accept_scope_and_receive_params(self):

self.assertEqual(request, expected_request)

def test_should_not_accept_scope_param_if_not_http_type(self):
from rollbar.contrib.starlette.requests import store_current_request

scope = {'asgi': {'spec_version': '2.0', 'version': '3.0'}, 'type': 'lifespan'}
receive = {}

request = store_current_request(scope, receive)

self.assertIsNone(request)

def test_hasuser(self):
from starlette.requests import Request
from rollbar.contrib.starlette.requests import hasuser
Expand Down

0 comments on commit 166b601

Please sign in to comment.