Skip to content

Commit

Permalink
improve(remote_images): handle connection error (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Jun 25, 2024
2 parents df4abaa + ec27de5 commit 1874755
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from mkdocs.structure.pages import Page
from mkdocs.utils import get_build_datetime
from requests import Session
from requests.exceptions import HTTPError
from requests.exceptions import ConnectionError, HTTPError

# package
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME, REMOTE_REQUEST_HEADERS
Expand Down Expand Up @@ -643,16 +643,20 @@ def get_remote_image_length(
# first, try HEAD request to avoid downloading the image
try:
attempt += 1
logger.debug(
f"Get remote image length (attempt {attempt}/2) - "
f"Sending {http_method} request to {image_url}"
)
req_response = self.req_session.request(
method=http_method, url=image_url, verify=ssl_verify
)
req_response.raise_for_status()
img_length = req_response.headers.get("content-length")
except HTTPError as err:
except (ConnectionError, HTTPError) as err:
logger.debug(
f"Remote image could not been reached: {image_url}. "
f"Trying again with GET and disabling SSL verification. Attempt: {attempt}. "
f"Trace: {err}"
f"Trying again with {http_method} and disabling SSL verification. "
f"Attempt: {attempt}/2. Trace: {err}"
)
if attempt < 2:
return self.get_remote_image_length(
Expand Down

0 comments on commit 1874755

Please sign in to comment.