diff --git a/scripts/west_commands/fetchers/http.py b/scripts/west_commands/fetchers/http.py index 817749541ca..3ff5eb6e815 100644 --- a/scripts/west_commands/fetchers/http.py +++ b/scripts/west_commands/fetchers/http.py @@ -2,7 +2,9 @@ # # SPDX-License-Identifier: Apache-2.0 +import os import requests +import sys from west import log @@ -16,5 +18,14 @@ class HTTPFetcher(ZephyrBlobFetcher): def fetch(self, url, path): log.dbg(f'HTTPFetcher fetching {url} to {path}') - resp = requests.get(url) - open(path, "wb").write(resp.content) + try: + resp = requests.get(url) + resp.raise_for_status() # Raises an HTTPError for bad status codes (4xx or 5xx) + except requests.exceptions.HTTPError as e: + log.err(f'HTTP error occurred: {e}') + sys.exit(os.EX_NOHOST) + except requests.exceptions.RequestException as e: + log.err(f'An error occurred: {e}') + sys.exit(os.EX_DATAERR) + with open(path, "wb") as f: + f.write(resp.content)