From 3bbc393773b13abbcdefec1d9cbcf5654425efef Mon Sep 17 00:00:00 2001 From: "bodong.yang" Date: Sat, 30 Nov 2024 13:09:30 +0000 Subject: [PATCH] ota_core: fix httperror handling --- src/otaclient/ota_core.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/otaclient/ota_core.py b/src/otaclient/ota_core.py index 28f7871be..a3d415b62 100644 --- a/src/otaclient/ota_core.py +++ b/src/otaclient/ota_core.py @@ -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 @@ -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__, @@ -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)