Skip to content

Commit

Permalink
Add LocalProtocolError, RemoteProtocolError (#129)
Browse files Browse the repository at this point in the history
* Add LocalProtocolError, RemoteProtocolError

* Fix indentation
  • Loading branch information
tomchristie committed Aug 2, 2020
1 parent 623f3c8 commit f4240b6
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 12 deletions.
4 changes: 4 additions & 0 deletions httpcore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
NetworkError,
PoolTimeout,
ProtocolError,
RemoteProtocolError,
LocalProtocolError,
ProxyError,
ReadError,
ReadTimeout,
Expand Down Expand Up @@ -39,6 +41,8 @@
"ReadError",
"WriteError",
"CloseError",
"LocalProtocolError",
"RemoteProtocolError",
"UnsupportedProtocol",
]
__version__ = "0.9.1"
4 changes: 3 additions & 1 deletion httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import AsyncIterator, Callable, Dict, List, Optional, Set, Tuple

from .._backends.auto import AsyncLock, AsyncSemaphore, AutoBackend
from .._exceptions import PoolTimeout, UnsupportedProtocol
from .._exceptions import PoolTimeout, LocalProtocolError, UnsupportedProtocol
from .._threadlock import ThreadLock
from .._types import URL, Headers, Origin, TimeoutDict
from .._utils import get_logger, origin_to_url_string, url_to_origin
Expand Down Expand Up @@ -127,6 +127,8 @@ async def request(
if url[0] not in (b"http", b"https"):
scheme = url[0].decode("latin-1")
raise UnsupportedProtocol(f"Unsupported URL protocol {scheme!r}")
if not url[1]:
raise LocalProtocolError("Missing hostname in URL.")

origin = url_to_origin(url)

Expand Down
7 changes: 4 additions & 3 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import h11

from .._backends.auto import AsyncSocketStream
from .._exceptions import ProtocolError, map_exceptions
from .._exceptions import RemoteProtocolError, LocalProtocolError, map_exceptions
from .._types import URL, Headers, TimeoutDict
from .._utils import get_logger
from .base import AsyncByteStream, ConnectionState
Expand Down Expand Up @@ -91,7 +91,8 @@ async def _send_request(
"""
logger.trace("send_request method=%r url=%r headers=%s", method, url, headers)
_scheme, _host, _port, target = url
event = h11.Request(method=method, target=target, headers=headers)
with map_exceptions({h11.LocalProtocolError: LocalProtocolError}):
event = h11.Request(method=method, target=target, headers=headers)
await self._send_event(event, timeout)

async def _send_request_body(
Expand Down Expand Up @@ -151,7 +152,7 @@ async def _receive_event(self, timeout: TimeoutDict) -> H11Event:
Read a single `h11` event, reading more data from the network if needed.
"""
while True:
with map_exceptions({h11.RemoteProtocolError: ProtocolError}):
with map_exceptions({h11.RemoteProtocolError: RemoteProtocolError}):
event = self.h11_state.next_event()

if event is h11.NEED_DATA:
Expand Down
4 changes: 2 additions & 2 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from h2.settings import SettingCodes, Settings

from .._backends.auto import AsyncLock, AsyncSemaphore, AsyncSocketStream, AutoBackend
from .._exceptions import PoolTimeout, ProtocolError
from .._exceptions import PoolTimeout, RemoteProtocolError
from .._types import URL, Headers, TimeoutDict
from .._utils import get_logger
from .base import AsyncByteStream, ConnectionState, NewConnectionRequired
Expand Down Expand Up @@ -215,7 +215,7 @@ async def receive_events(self, timeout: TimeoutDict) -> None:
logger.trace("receive_event stream_id=%r event=%s", event_stream_id, event)

if hasattr(event, "error_code"):
raise ProtocolError(event)
raise RemoteProtocolError(event)

if event_stream_id in self.events:
self.events[event_stream_id].append(event)
Expand Down
8 changes: 8 additions & 0 deletions httpcore/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class ProtocolError(Exception):
pass


class RemoteProtocolError(ProtocolError):
pass


class LocalProtocolError(ProtocolError):
pass


class ProxyError(Exception):
pass

Expand Down
4 changes: 3 additions & 1 deletion httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Iterator, Callable, Dict, List, Optional, Set, Tuple

from .._backends.auto import SyncLock, SyncSemaphore, SyncBackend
from .._exceptions import PoolTimeout, UnsupportedProtocol
from .._exceptions import PoolTimeout, LocalProtocolError, UnsupportedProtocol
from .._threadlock import ThreadLock
from .._types import URL, Headers, Origin, TimeoutDict
from .._utils import get_logger, origin_to_url_string, url_to_origin
Expand Down Expand Up @@ -127,6 +127,8 @@ def request(
if url[0] not in (b"http", b"https"):
scheme = url[0].decode("latin-1")
raise UnsupportedProtocol(f"Unsupported URL protocol {scheme!r}")
if not url[1]:
raise LocalProtocolError("Missing hostname in URL.")

origin = url_to_origin(url)

Expand Down
7 changes: 4 additions & 3 deletions httpcore/_sync/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import h11

from .._backends.auto import SyncSocketStream
from .._exceptions import ProtocolError, map_exceptions
from .._exceptions import RemoteProtocolError, LocalProtocolError, map_exceptions
from .._types import URL, Headers, TimeoutDict
from .._utils import get_logger
from .base import SyncByteStream, ConnectionState
Expand Down Expand Up @@ -91,7 +91,8 @@ def _send_request(
"""
logger.trace("send_request method=%r url=%r headers=%s", method, url, headers)
_scheme, _host, _port, target = url
event = h11.Request(method=method, target=target, headers=headers)
with map_exceptions({h11.LocalProtocolError: LocalProtocolError}):
event = h11.Request(method=method, target=target, headers=headers)
self._send_event(event, timeout)

def _send_request_body(
Expand Down Expand Up @@ -151,7 +152,7 @@ def _receive_event(self, timeout: TimeoutDict) -> H11Event:
Read a single `h11` event, reading more data from the network if needed.
"""
while True:
with map_exceptions({h11.RemoteProtocolError: ProtocolError}):
with map_exceptions({h11.RemoteProtocolError: RemoteProtocolError}):
event = self.h11_state.next_event()

if event is h11.NEED_DATA:
Expand Down
4 changes: 2 additions & 2 deletions httpcore/_sync/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from h2.settings import SettingCodes, Settings

from .._backends.auto import SyncLock, SyncSemaphore, SyncSocketStream, SyncBackend
from .._exceptions import PoolTimeout, ProtocolError
from .._exceptions import PoolTimeout, RemoteProtocolError
from .._types import URL, Headers, TimeoutDict
from .._utils import get_logger
from .base import SyncByteStream, ConnectionState, NewConnectionRequired
Expand Down Expand Up @@ -215,7 +215,7 @@ def receive_events(self, timeout: TimeoutDict) -> None:
logger.trace("receive_event stream_id=%r event=%s", event_stream_id, event)

if hasattr(event, "error_code"):
raise ProtocolError(event)
raise RemoteProtocolError(event)

if event_stream_id in self.events:
self.events[event_stream_id].append(event)
Expand Down

0 comments on commit f4240b6

Please sign in to comment.