Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This document records all notable changes to [HTTPie](https://httpie.io).
This project adheres to [Semantic Versioning](https://semver.org/).

## Unreleased

- Prevent long or cyclic exception context chains from crashing connection error reporting.

## [3.2.4](https://github.com/httpie/cli/compare/3.2.3...3.2.4) (2024-11-01)

- Fix default certs loading and unpin `requests`. ([#1596](https://github.com/httpie/cli/issues/1596))
Expand Down
14 changes: 9 additions & 5 deletions httpie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,15 @@ def split_iterable(iterable: Iterable[T], key: Callable[[T], bool]) -> Tuple[Lis


def unwrap_context(exc: Exception) -> Optional[Exception]:
context = exc.__context__
if isinstance(context, Exception):
return unwrap_context(context)
else:
return exc
"""Find the deepest exception context, stopping before a cycle repeats."""
seen = {id(exc)}
while isinstance(exc.__context__, Exception):
context = exc.__context__
if id(context) in seen:
break
seen.add(id(context))
exc = context
return exc


def url_as_host(url: str) -> str:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import pytest
import socket
import sys
from unittest import mock
from pytest import raises
from requests import Request
from requests.exceptions import ConnectionError

from httpie.status import ExitStatus
from httpie.utils import unwrap_context
from .utils import HTTP_OK, http


Expand Down Expand Up @@ -48,6 +50,48 @@ def test_error_custom_dns(program, error_code, expected_message):
assert expected_message in r.stderr


@pytest.mark.parametrize('depth', [0, 1, sys.getrecursionlimit() + 1])
def test_unwrap_context(depth):
original = ValueError('Original error')
exc = original
for _ in range(depth):
wrapper = RuntimeError('Wrapper')
wrapper.__context__ = exc
exc = wrapper

assert unwrap_context(exc) is original


def test_unwrap_context_stops_at_base_exception():
exc = ValueError('Original error')
exc.__context__ = KeyboardInterrupt()

assert unwrap_context(exc) is exc


@pytest.mark.parametrize('length', [1, 2, 3])
def test_unwrap_context_cycle(length):
errors = [ValueError('Error') for _ in range(length)]
for index, error in enumerate(errors):
error.__context__ = errors[(index + 1) % length]

assert unwrap_context(errors[0]) is errors[-1]


@mock.patch('httpie.core.program')
def test_error_custom_dns_deep_context(program):
exc = socket.gaierror(socket.EAI_NONAME, '<test>')
for _ in range(sys.getrecursionlimit() + 1):
wrapper = ConnectionError('Connection aborted')
wrapper.__context__ = exc
exc = wrapper
program.side_effect = exc

r = http('example.org', tolerate_error_exit_status=True)
assert r.exit_status == ExitStatus.ERROR
assert 'check the URL' in r.stderr


def test_max_headers_limit(httpbin_both):
with raises(ConnectionError) as e:
http('--max-headers=1', httpbin_both + '/get')
Expand Down
Loading