From 8e6477f404e97f1c4b0a0516a677953f93f79f90 Mon Sep 17 00:00:00 2001 From: Mladen Gibanica <11275336+mgcth@users.noreply.github.com> Date: Sat, 6 Apr 2024 16:48:25 +0200 Subject: [PATCH] Add error message for out of bounds request --- src/smhi/constants.py | 1 + src/smhi/utils.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/smhi/constants.py b/src/smhi/constants.py index aa8229d0..d36e5add 100644 --- a/src/smhi/constants.py +++ b/src/smhi/constants.py @@ -157,3 +157,4 @@ def get_now() -> datetime: STRANG_TIME_INTERVALS = ["hourly", "daily", "monthly"] STATUS_OK = 200 +OUT_OF_BOUNDS = "out of bounds" diff --git a/src/smhi/utils.py b/src/smhi/utils.py index 5d50f4c0..b3f0fafc 100644 --- a/src/smhi/utils.py +++ b/src/smhi/utils.py @@ -3,7 +3,7 @@ import logging import requests -from smhi.constants import STATUS_OK +from smhi.constants import OUT_OF_BOUNDS, STATUS_OK logger = logging.getLogger(__name__) @@ -18,6 +18,7 @@ def get_request(url: str) -> requests.Response: response Raises: + ValueError requests.exceptions.HTTPError """ logger.debug(f"Fetching from {url}.") @@ -25,7 +26,10 @@ def get_request(url: str) -> requests.Response: response = requests.get(url, timeout=200) if response.status_code != STATUS_OK: - raise requests.exceptions.HTTPError(f"Could request from {url}.") + if OUT_OF_BOUNDS in response.text.lower(): + raise ValueError("Request is out of bounds.") + + raise requests.exceptions.HTTPError(f"Could not request from {url}.") logger.debug(f"Successful request from {url}.")