Skip to content

fix(wsgi): Avoid adding extra parameters if not necessary #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2020
Merged
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
28 changes: 22 additions & 6 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@
from typing import Tuple
from typing import Optional
from typing import TypeVar
from typing import Protocol

from sentry_sdk.utils import ExcInfo
from sentry_sdk._types import EventProcessor

T = TypeVar("T")
U = TypeVar("U")
E = TypeVar("E")
WsgiResponseIter = TypeVar("WsgiResponseIter")
WsgiResponseHeaders = TypeVar("WsgiResponseHeaders")
WsgiExcInfo = TypeVar("WsgiExcInfo")

class StartResponse(Protocol):
def __call__(self, status, response_headers, exc_info=None):
# type: (str, WsgiResponseHeaders, Optional[WsgiExcInfo]) -> WsgiResponseIter
pass


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


def _sentry_start_response(
old_start_response, span, status, response_headers, exc_info=None
old_start_response, # type: StartResponse
span, # type: Span
status, # type: str
response_headers, # type: WsgiResponseHeaders
exc_info=None, # type: Optional[WsgiExcInfo]
):
# type: (Callable[[str, U, Optional[E]], T], Span, str, U, Optional[E]) -> T
# type: (...) -> WsgiResponseIter
with capture_internal_exceptions():
status_int = int(status.split(" ", 1)[0])
span.set_http_status(status_int)

return old_start_response(status, response_headers, exc_info)
if exc_info is None:
# The Django Rest Framework WSGI test client, and likely other
# (incorrect) implementations, cannot deal with the exc_info argument
# if one is present. Avoid providing a third argument if not necessary.
return old_start_response(status, response_headers)
else:
return old_start_response(status, response_headers, exc_info)


def _get_environ(environ):
Expand Down