Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve(remote_images): handle connection error #307

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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