|
21 | 21 | from typing import Tuple
|
22 | 22 | from typing import Optional
|
23 | 23 | from typing import TypeVar
|
| 24 | + from typing import Protocol |
24 | 25 |
|
25 | 26 | from sentry_sdk.utils import ExcInfo
|
26 | 27 | from sentry_sdk._types import EventProcessor
|
27 | 28 |
|
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 |
31 | 37 |
|
32 | 38 |
|
33 | 39 | _wsgi_middleware_applied = ContextVar("sentry_wsgi_middleware_applied")
|
@@ -125,14 +131,24 @@ def __call__(self, environ, start_response):
|
125 | 131 |
|
126 | 132 |
|
127 | 133 | 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] |
129 | 139 | ):
|
130 |
| - # type: (Callable[[str, U, Optional[E]], T], Span, str, U, Optional[E]) -> T |
| 140 | + # type: (...) -> WsgiResponseIter |
131 | 141 | with capture_internal_exceptions():
|
132 | 142 | status_int = int(status.split(" ", 1)[0])
|
133 | 143 | span.set_http_status(status_int)
|
134 | 144 |
|
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) |
136 | 152 |
|
137 | 153 |
|
138 | 154 | def _get_environ(environ):
|
|
0 commit comments