Skip to content
Myr

Wrap decoding errors with custom exceptions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yeray Diaz Diaz · committed by Yeray Diaz Diaz · parent 91a2a1b · Browse files · 4 files+71 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+71 added−8 removed

  • tests:+15 added−0 removed
  • other:+56 added−8 removed

4 files+71 added−8 removed

modified:httpcore/__init__.py+3 added
7 unmodified lines
8 ReadTimeout,9 ResponseClosed,10 StreamConsumed,11 Timeout,12 DeflateDecodingError,13 GzipDecodingError,14 BrotliDecodingError,15)16from .http11 import HTTP11Connection17from .sync import SyncClient, SyncConnectionPool18
1 unmodified line
modified:httpcore/decoders.py+29 added-8 removed
10 unmodified lines
11except ImportError: # pragma: nocover12 brotli = None131415import httpcore.exceptions161718class Decoder:19 def decode(self, data: bytes) -> bytes:20 raise NotImplementedError() # pragma: nocover21
19 unmodified lines
41 def __init__(self) -> None:42 self.decompressor = zlib.decompressobj(-zlib.MAX_WBITS)4344 def decode(self, data: bytes) -> bytes:45 try:42 return self.decompressor.decompress(data)46 return self.decompressor.decompress(data)47 except zlib.error as exc:48 raise httpcore.exceptions.DeflateDecodingError from exc4950 def flush(self) -> bytes:51 try:45 return self.decompressor.flush()52 return self.decompressor.flush()53 except zlib.error as exc:54 raise httpcore.exceptions.DeflateDecodingError from exc555657class GZipDecoder(Decoder):58 """
5 unmodified lines
64 def __init__(self) -> None:65 self.decompressor = zlib.decompressobj(zlib.MAX_WBITS | 16)6667 def decode(self, data: bytes) -> bytes:68 try:59 return self.decompressor.decompress(data)69 return self.decompressor.decompress(data)70 except zlib.error as exc:71 raise httpcore.exceptions.GzipDecodingError from exc7273 def flush(self) -> bytes:74 try:62 return self.decompressor.flush()75 return self.decompressor.flush()76 except zlib.error as exc:77 raise httpcore.exceptions.GzipDecodingError from exc787980class BrotliDecoder(Decoder):81 """
9 unmodified lines
91 ), "The 'brotlipy' library must be installed to use 'BrotliDecoder'"92 self.decompressor = brotli.Decompressor()9394 def decode(self, data: bytes) -> bytes:95 try:80 return self.decompressor.decompress(data)96 return self.decompressor.decompress(data)97 except brotli.Error as exc:98 raise httpcore.exceptions.BrotliDecodingError from exc99100 def flush(self) -> bytes:101 try:83 self.decompressor.finish()84 return b""102 self.decompressor.finish()103 return b""104 except brotli.Error as exc:105 raise httpcore.exceptions.BrotliDecodingError from exc106107108class MultiDecoder(Decoder):109 """89 Handle the case where mutliple encodings have been applied.110 Handle the case where mutiple encodings have been applied.111 """112113 def __init__(self, children: typing.Sequence[Decoder]) -> None:114 """
33 unmodified lines
modified:httpcore/exceptions.py+24 added
38 unmodified lines
39 """40 Attempted to read or stream response content, but the request has been41 closed without loading the body.42 """434445class DecodingError(Exception):46 """47 Decoding of the response failed.48 """495051class DeflateDecodingError(DecodingError):52 """53 Decoding of the response using deflate failed.54 """555657class GzipDecodingError(DecodingError):58 """59 Decoding of the response using gzip failed.60 """616263class BrotliDecodingError(DecodingError):64 """65 Decoding of the response using brotli failed.66 """
modified:tests/test_decoding.py+15 added
75 unmodified lines
76 headers = [(b"Content-Encoding", b"gzip")]77 response = httpcore.Response(200, headers=headers, body=compress(body))78 assert not hasattr(response, "body")79 assert await response.read() == body808182@pytest.mark.parametrize(83 'header_value, expected_exception',84 [85 (b"deflate", httpcore.DeflateDecodingError),86 (b"gzip", httpcore.GzipDecodingError),87 (b"br", httpcore.BrotliDecodingError),88 ])89def test_decoding_errors(header_value, expected_exception):90 headers = [(b"Content-Encoding", header_value)]91 body = b"test 123"92 compressed_body = brotli.compress(body)[3:]93 with pytest.raises(expected_exception):94 response = httpcore.Response(200, headers=headers, body=compressed_body)