Skip to content

Commit

Permalink
handle exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zrquan committed Sep 13, 2024
1 parent 0f91c8e commit 50a25ad
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions lib/connection/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import random
import re
import socket
from ssl import SSLError
import threading
import time
from typing import Generator
Expand Down Expand Up @@ -377,26 +378,23 @@ async def request(self, path: str) -> AsyncResponse:
except Exception as e:
logger.exception(e)

if e == socket.gaierror:
err_msg = "Couldn't resolve DNS"
elif "SSLError" in str(e):
if isinstance(e, httpx.ConnectError):
if str(e).startswith("[Errno -2]"):
err_msg = "Couldn't resolve DNS"
else:
err_msg = f"Cannot connect to: {urlparse(url).netloc}"
elif isinstance(e, SSLError):
err_msg = "Unexpected SSL error"
elif "TooManyRedirects" in str(e):
elif isinstance(e, httpx.TooManyRedirects):
err_msg = f"Too many redirects: {url}"
elif "ProxyError" in str(e):
err_msg = "Error with the system proxy"
elif "InvalidURL" in str(e):
elif isinstance(e, httpx.ProxyError):
err_msg = "Cannot establish the proxy connection"
elif isinstance(e, httpx.InvalidURL):
err_msg = f"Invalid URL: {url}"
elif "ConnectionError" in str(e):
err_msg = f"Cannot connect to: {urlparse(url).netloc}"
elif re.search(READ_RESPONSE_ERROR_REGEX, str(e)):
err_msg = f"Failed to read response body: {url}"
elif "Timeout" in str(e) or e in (
httpx.ConnectTimeout,
httpx.ReadTimeout,
socket.timeout,
):
elif isinstance(e, httpx.TimeoutException):
err_msg = f"Request timeout: {url}"
elif isinstance(e, httpx.ReadError) or isinstance(e, httpx.DecodingError): # not sure
err_msg = f"Failed to read response body: {url}"
else:
err_msg = f"There was a problem in the request to: {url}"

Expand Down

0 comments on commit 50a25ad

Please sign in to comment.