Merge pull request #20 from yeraydiazdiaz/decoding-exceptions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Wrap decoding errors with custom exceptions
authored by Tom Christie · committed by GitHub · parents 696c054, 51ae3e1 · Browse files · 4 files·+46 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+46 added−8 removed
- tests:+9 added−0 removed
- other:+37 added−8 removed
Provenance
- Commit
2b134a6- Tree
6741023- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent 1
696c054- Parent 2
51ae3e1
4 files changed+46 added−8 removed
modified:httpcore/__init__.py+1 added
5 unmodified lines6 from .dispatch.http2 import HTTP2Connection7 from .dispatch.http11 import HTTP11Connection8 from .exceptions import (9 ConnectTimeout,10+ DecodingError,11 PoolTimeout,12 ProtocolError,13 ReadTimeout,14 RedirectBodyUnavailable,13 unmodified linesmodified:httpcore/decoders.py+29 added-8 removed
10 unmodified lines11 except ImportError: # pragma: nocover12 brotli = None13 14 15+import httpcore.exceptions16+17+18 class Decoder:19 def decode(self, data: bytes) -> bytes:20 raise NotImplementedError() # pragma: nocover21 19 unmodified lines41 def __init__(self) -> None:42 self.decompressor = zlib.decompressobj(-zlib.MAX_WBITS)43 44 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.DecodingError from exc49 50 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.DecodingError from exc55 56 57 class GZipDecoder(Decoder):58 """5 unmodified lines64 def __init__(self) -> None:65 self.decompressor = zlib.decompressobj(zlib.MAX_WBITS | 16)66 67 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.DecodingError from exc72 73 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.DecodingError from exc78 79 80 class BrotliDecoder(Decoder):81 """9 unmodified lines91 ), "The 'brotlipy' library must be installed to use 'BrotliDecoder'"92 self.decompressor = brotli.Decompressor()93 94 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.DecodingError from exc99 100 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.DecodingError from exc106 107 108 class MultiDecoder(Decoder):109 """89- Handle the case where mutliple encodings have been applied.110+ Handle the case where multiple encodings have been applied.111 """112 113 def __init__(self, children: typing.Sequence[Decoder]) -> None:114 """33 unmodified linesmodified:httpcore/exceptions.py+7 added
71 unmodified lines72 closed without loading the body.73 """74 75 76+class DecodingError(Exception):77+ """78+ Decoding of the response failed.79+ """80+81+ 82 class InvalidURL(Exception):83 """84+ URL was missing a hostname, or was not one of HTTP/HTTPS.85 """modified:tests/test_decoding.py+9 added
75 unmodified lines76 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() == body80+81+82+@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))83+def test_decoding_errors(header_value):84+ headers = [(b"Content-Encoding", header_value)]85+ body = b"test 123"86+ compressed_body = brotli.compress(body)[3:]87+ with pytest.raises(httpcore.exceptions.DecodingError):88+ response = httpcore.Response(200, headers=headers, body=compressed_body)