Skip to content

Instantly share code, notes, and snippets.

@injust
Last active April 29, 2025 11:47
Show Gist options
  • Save injust/6e0c1c30baa2797c0c4f354c2b5b7f07 to your computer and use it in GitHub Desktop.
Save injust/6e0c1c30baa2797c0c4f354c2b5b7f07 to your computer and use it in GitHub Desktop.
httpx: Remove `HTTPStatusError` info suffix from `raise_for_status()`
from collections.abc import Callable
from functools import wraps
import httpx
def httpx_remove_HTTPStatusError_info_suffix(
raise_for_status: Callable[[httpx.Response], httpx.Response],
) -> Callable[[httpx.Response], httpx.Response]:
@wraps(raise_for_status)
def wrapper(self: httpx.Response) -> httpx.Response:
try:
return raise_for_status(self)
except httpx.HTTPStatusError as e:
assert len(e.args) == 1 and isinstance(e.args[0], str), e.args
message, removed = e.args[0].rsplit("\n", 1)
assert removed.startswith("For more information check:"), removed
e.args = (message,)
raise
return wrapper
httpx.Response.raise_for_status = remove_HTTPStatusError_info_suffix(httpx.Response.raise_for_status) # type: ignore[assignment, method-assign] # pyright: ignore[reportAttributeAccessIssue]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment