diff --git a/cumulus_library/cli.py b/cumulus_library/cli.py index 9dc37cb8..3055ecdb 100755 --- a/cumulus_library/cli.py +++ b/cumulus_library/cli.py @@ -7,6 +7,7 @@ import sys import sysconfig +import requests import rich from cumulus_library import ( @@ -318,7 +319,11 @@ def run_cli(args: dict): create_template(args["create_dir"]) elif args["action"] == "upload": - upload.upload_files(args) + try: + upload.upload_files(args) + except requests.RequestException as e: + print(str(e)) + sys.exit() # all other actions require connecting to the database else: diff --git a/cumulus_library/upload.py b/cumulus_library/upload.py index b0489e02..0ed6ea52 100644 --- a/cumulus_library/upload.py +++ b/cumulus_library/upload.py @@ -42,9 +42,8 @@ def upload_data( if prefetch_res.status_code != 200: print("Invalid user/site id") - raise requests.RequestException(response=prefetch_res) + prefetch_res.raise_for_status() res_body = prefetch_res.json() - with open(file_path, "rb") as data_file: files = {"file": (file_name, data_file)} upload_req = requests.Request( @@ -55,7 +54,7 @@ def upload_data( upload_res = s.send(upload_req, timeout=60) if upload_res.status_code != 204: print(f"Error uploading {study}/{file_name}") - raise requests.RequestException(response=upload_res) + upload_res.raise_for_status() else: print("upload_req") print("headers", upload_req.headers) diff --git a/tests/test_cli.py b/tests/test_cli.py index 17700507..ccc120fa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -15,7 +15,6 @@ from unittest import mock import pytest -import requests import requests_mock import toml @@ -507,13 +506,13 @@ def test_cli_stats_rebuild(tmp_path): ["upload", "--user", "user", "--id", "id"], 500, False, - pytest.raises(requests.exceptions.RequestException), + pytest.raises(SystemExit), ), ( ["upload", "--user", "baduser", "--id", "badid"], 204, True, - pytest.raises(requests.exceptions.RequestException), + pytest.raises(SystemExit), ), ( ["upload", "--user", "user", "--id", "id", "./foo"],