diff --git a/gidgethub/__init__.py b/gidgethub/__init__.py index a8567ed..396dfe3 100644 --- a/gidgethub/__init__.py +++ b/gidgethub/__init__.py @@ -3,7 +3,7 @@ __version__ = "5.4.0.dev" import http -from typing import Any, Optional +from typing import Any, Mapping, Optional class GitHubException(Exception): @@ -19,8 +19,14 @@ class ValidationFailure(GitHubException): class HTTPException(GitHubException): """A general exception to represent HTTP responses.""" - def __init__(self, status_code: http.HTTPStatus, *args: Any) -> None: + def __init__( + self, + status_code: http.HTTPStatus, + *args: Any, + headers: Optional[Mapping[str, str | None]] = None, + ) -> None: self.status_code = status_code + self.headers = headers or {} if args: super().__init__(*args) else: diff --git a/gidgethub/sansio.py b/gidgethub/sansio.py index 95830f6..1c2bead 100644 --- a/gidgethub/sansio.py +++ b/gidgethub/sansio.py @@ -377,7 +377,7 @@ def decipher_response( args = status_code_enum, message else: args = (status_code_enum,) - raise exc_type(*args) + raise exc_type(*args, headers=headers) DOMAIN = "https://api.github.com" diff --git a/tests/test_sansio.py b/tests/test_sansio.py index 1139adb..2fbe039 100644 --- a/tests/test_sansio.py +++ b/tests/test_sansio.py @@ -416,8 +416,10 @@ def test_422_html_response(self): def test_3XX(self): status_code = 301 with pytest.raises(RedirectionException) as exc_info: - sansio.decipher_response(status_code, {}, b"") + headers = {"location": "https://api.github.com/test"} + sansio.decipher_response(status_code, headers, b"") assert exc_info.value.status_code == http.HTTPStatus(status_code) + assert exc_info.value.headers == headers def test_2XX_error(self): status_code = 205