Skip to content

Commit

Permalink
ota_core: fix httperror handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Nov 30, 2024
1 parent ca60a0e commit 3bbc393
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/otaclient/ota_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from urllib.parse import urlparse

import requests.exceptions as requests_exc
from requests import Response

from ota_metadata.legacy import parser as ota_metadata_parser
from ota_metadata.legacy import types as ota_metadata_types
Expand Down Expand Up @@ -124,12 +125,15 @@ def _download_exception_handler(_fut: Future[Any]) -> bool:
try:
# exceptions that cannot be handled by us
if isinstance(exc, requests_exc.HTTPError):
http_errcode = exc.errno

if http_errcode in [
HTTPStatus.FORBIDDEN,
HTTPStatus.UNAUTHORIZED,
]:
_response = exc.response
# NOTE(20241129): if somehow HTTPError doesn't contain response,
# don't do anything but let upper retry.
# NOTE: bool(Response) is False when status_code != 200.
if not isinstance(_response, Response):
return False

http_errcode = _response.status_code
if http_errcode in [HTTPStatus.FORBIDDEN, HTTPStatus.UNAUTHORIZED]:
raise ota_errors.UpdateRequestCookieInvalid(
f"download failed with critical HTTP error: {exc.errno}, {exc!r}",
module=__name__,
Expand Down Expand Up @@ -460,6 +464,16 @@ def _execute_update(self):
_err_msg = f"metadata.jwt is invalid: {e!r}"
logger.error(_err_msg)
raise ota_errors.MetadataJWTInvalid(_err_msg, module=__name__) from e
except ota_metadata_parser.OTAImageInvalid as e:
_err_msg = f"OTA image is invalid: {e!r}"
logger.error(_err_msg)
raise ota_errors.OTAImageInvalid(_err_msg, module=__name__) from e
except ota_metadata_parser.OTARequestsAuthTokenInvalid as e:
_err_msg = f"OTA requests auth token is invalid: {e!r}"
logger.error(_err_msg)
raise ota_errors.UpdateRequestCookieInvalid(
_err_msg, module=__name__
) from e
except Exception as e:
_err_msg = f"failed to prepare ota metafiles: {e!r}"
logger.error(_err_msg)
Expand Down

0 comments on commit 3bbc393

Please sign in to comment.