Skip to content

Commit

Permalink
make DownloadFailedExceptions carry original exception for richer inf…
Browse files Browse the repository at this point in the history
…ormation (#279)
  • Loading branch information
zigaLuksic committed May 26, 2022
1 parent 1aa8b2e commit b73ce6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 7 additions & 3 deletions sentinelhub/download/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def new_download_func(self: Self, request: DownloadRequest) -> T:
and exception.response.status_code != requests.status_codes.codes.TOO_MANY_REQUESTS
):

raise DownloadFailedException(_create_download_failed_message(exception, request.url)) from exception
raise DownloadFailedException(
_create_download_failed_message(exception, request.url), request_exception=exception
) from exception
raise exception from exception

return new_download_func
Expand Down Expand Up @@ -77,7 +79,7 @@ def new_download_func(self: SelfWithConfig, request: DownloadRequest) -> T:

if attempt_num == download_attempts - 1:
message = _create_download_failed_message(exception, request.url)
raise DownloadFailedException(message) from exception
raise DownloadFailedException(message, request_exception=exception) from exception

LOGGER.debug(
"Download attempt failed: %s\n%d attempts left, will retry in %ds",
Expand All @@ -103,7 +105,9 @@ def new_download_func(self: Self, request: DownloadRequest) -> T:
return download_func(self, request)
except requests.HTTPError as exception:
if exception.response.status_code == requests.status_codes.codes.NOT_FOUND:
raise DownloadFailedException(f"File in location {request.url} is missing") from exception
raise DownloadFailedException(
f"File in location {request.url} is missing", request_exception=exception
) from exception

raise exception from exception

Expand Down
8 changes: 7 additions & 1 deletion sentinelhub/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
Module defining custom package exceptions
"""

import warnings
from typing import Optional

import requests


class BaseSentinelHubException(Exception):
Expand All @@ -12,6 +14,10 @@ class BaseSentinelHubException(Exception):
class DownloadFailedException(BaseSentinelHubException):
"""General exception which is raised whenever download fails"""

def __init__(self, msg: str, *, request_exception: Optional[requests.RequestException] = None) -> None:
super().__init__(msg)
self.request_exception = request_exception


class OutOfRequestsException(DownloadFailedException):
"""An exception raised whenever download cannot be done because user has run out of requests or processing units"""
Expand Down

0 comments on commit b73ce6d

Please sign in to comment.