Skip to content

Commit 9a42f95

Browse files
authored
fix(wsgi): Avoid adding extra parameters if not necessary (getsentry#588)
* fix(wsgi): Avoid adding extra parameters if not necessary * fix: Linters
1 parent 39ea175 commit 9a42f95

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

sentry_sdk/integrations/wsgi.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@
2121
from typing import Tuple
2222
from typing import Optional
2323
from typing import TypeVar
24+
from typing import Protocol
2425

2526
from sentry_sdk.utils import ExcInfo
2627
from sentry_sdk._types import EventProcessor
2728

28-
T = TypeVar("T")
29-
U = TypeVar("U")
30-
E = TypeVar("E")
29+
WsgiResponseIter = TypeVar("WsgiResponseIter")
30+
WsgiResponseHeaders = TypeVar("WsgiResponseHeaders")
31+
WsgiExcInfo = TypeVar("WsgiExcInfo")
32+
33+
class StartResponse(Protocol):
34+
def __call__(self, status, response_headers, exc_info=None):
35+
# type: (str, WsgiResponseHeaders, Optional[WsgiExcInfo]) -> WsgiResponseIter
36+
pass
3137

3238

3339
_wsgi_middleware_applied = ContextVar("sentry_wsgi_middleware_applied")
@@ -125,14 +131,24 @@ def __call__(self, environ, start_response):
125131

126132

127133
def _sentry_start_response(
128-
old_start_response, span, status, response_headers, exc_info=None
134+
old_start_response, # type: StartResponse
135+
span, # type: Span
136+
status, # type: str
137+
response_headers, # type: WsgiResponseHeaders
138+
exc_info=None, # type: Optional[WsgiExcInfo]
129139
):
130-
# type: (Callable[[str, U, Optional[E]], T], Span, str, U, Optional[E]) -> T
140+
# type: (...) -> WsgiResponseIter
131141
with capture_internal_exceptions():
132142
status_int = int(status.split(" ", 1)[0])
133143
span.set_http_status(status_int)
134144

135-
return old_start_response(status, response_headers, exc_info)
145+
if exc_info is None:
146+
# The Django Rest Framework WSGI test client, and likely other
147+
# (incorrect) implementations, cannot deal with the exc_info argument
148+
# if one is present. Avoid providing a third argument if not necessary.
149+
return old_start_response(status, response_headers)
150+
else:
151+
return old_start_response(status, response_headers, exc_info)
136152

137153

138154
def _get_environ(environ):

0 commit comments

Comments
 (0)