Skip to content

Commit 3ff74f7

Browse files
feat: refactor exception handling
1 parent 210e9e9 commit 3ff74f7

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

oc4ids_datastore_pipeline/storage.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,61 +28,58 @@ def _get_client() -> Any:
2828
)
2929

3030

31-
def _upload_file(local_path: str, bucket_path: str, content_type: str) -> str:
32-
logger.info(f"Uploading file {local_path}")
33-
client = _get_client()
34-
client.upload_file(
35-
local_path,
36-
BUCKET_NAME,
37-
bucket_path,
38-
ExtraArgs={"ACL": "public-read", "ContentType": content_type},
39-
)
40-
public_url = (
41-
f"https://{BUCKET_NAME}.{BUCKET_REGION}.digitaloceanspaces.com/" + bucket_path
42-
)
43-
logger.info(f"Uploaded to {public_url}")
44-
return public_url
45-
46-
47-
def _upload_json(dataset_id: str, json_path: str) -> Optional[str]:
31+
def _upload_file(local_path: str, bucket_path: str, content_type: str) -> Optional[str]:
4832
try:
49-
return _upload_file(
50-
local_path=json_path,
51-
bucket_path=f"{dataset_id}/{dataset_id}.json",
52-
content_type="application/json",
33+
logger.info(f"Uploading file {local_path}")
34+
client = _get_client()
35+
client.upload_file(
36+
local_path,
37+
BUCKET_NAME,
38+
bucket_path,
39+
ExtraArgs={"ACL": "public-read", "ContentType": content_type},
5340
)
41+
public_url = (
42+
f"https://{BUCKET_NAME}.{BUCKET_REGION}.digitaloceanspaces.com/"
43+
+ bucket_path
44+
)
45+
logger.info(f"Uploaded to {public_url}")
46+
return public_url
5447
except Exception as e:
55-
logger.warning(f"Failed to upload {json_path} with error {e}")
48+
logger.warning(f"Failed to upload {local_path} with error {e}")
5649
return None
5750

5851

52+
def _upload_json(dataset_id: str, json_path: str) -> Optional[str]:
53+
return _upload_file(
54+
local_path=json_path,
55+
bucket_path=f"{dataset_id}/{dataset_id}.json",
56+
content_type="application/json",
57+
)
58+
59+
5960
def _upload_csv(dataset_id: str, csv_path: str) -> Optional[str]:
6061
try:
6162
directory = Path(csv_path)
6263
zip_file_path = f"{csv_path}_csv.zip"
6364
with zipfile.ZipFile(zip_file_path, mode="w") as archive:
6465
for file_path in directory.rglob("*"):
6566
archive.write(file_path, arcname=file_path.relative_to(directory))
66-
return _upload_file(
67-
local_path=zip_file_path,
68-
bucket_path=f"{dataset_id}/{dataset_id}_csv.zip",
69-
content_type="application/zip",
70-
)
7167
except Exception as e:
72-
logger.warning(f"Failed to upload {csv_path} with error {e}")
68+
logger.warning(f"Failed to zip {csv_path} with error {e}")
7369
return None
70+
return _upload_file(
71+
local_path=zip_file_path,
72+
bucket_path=f"{dataset_id}/{dataset_id}_csv.zip",
73+
content_type="application/zip",
74+
)
7475

7576

7677
def _upload_xlsx(dataset_id: str, xlsx_path: str) -> Optional[str]:
77-
try:
78-
return _upload_file(
79-
local_path=xlsx_path,
80-
bucket_path=f"{dataset_id}/{dataset_id}.xlsx",
81-
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # noqa: E501
82-
)
83-
except Exception as e:
84-
logger.warning(f"Failed to upload {xlsx_path} with error {e}")
85-
return None
78+
return _upload_file(
79+
local_path=xlsx_path,
80+
bucket_path=f"{dataset_id}/{dataset_id}.xlsx",
81+
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # noqa: E501
82+
)
8683

8784

8885
def upload_files(

tests/test_storage.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_upload_files_json(mock_client: MagicMock) -> None:
6464
assert xlsx_public_url is None
6565

6666

67-
def test_upload_files_json_catches_exception(mock_client: MagicMock) -> None:
67+
def test_upload_files_json_catches_upload_exception(mock_client: MagicMock) -> None:
6868
mock_client.upload_file.side_effect = [Exception("Mock exception"), None, None]
6969

7070
with tempfile.TemporaryDirectory() as csv_dir:
@@ -105,7 +105,25 @@ def test_upload_files_csv(mock_client: MagicMock) -> None:
105105
assert xlsx_public_url is None
106106

107107

108-
def test_upload_files_csv_catches_exception(mock_client: MagicMock) -> None:
108+
def test_upload_files_csv_catches_zip_exception() -> None:
109+
json_public_url, csv_public_url, xlsx_public_url = upload_files(
110+
"test_dataset",
111+
json_path="data/test_dataset/test_dataset.json",
112+
csv_path="non/existent/directory",
113+
xlsx_path="data/test_dataset/test_dataset.xlsx",
114+
)
115+
assert (
116+
json_public_url
117+
== "https://test-bucket.test-region.digitaloceanspaces.com/test_dataset/test_dataset.json" # noqa: E501
118+
)
119+
assert csv_public_url is None
120+
assert (
121+
xlsx_public_url
122+
== "https://test-bucket.test-region.digitaloceanspaces.com/test_dataset/test_dataset.xlsx" # noqa: E501
123+
)
124+
125+
126+
def test_upload_files_csv_catches_upload_exception(mock_client: MagicMock) -> None:
109127
mock_client.upload_file.side_effect = [None, Exception("Mock exception"), None]
110128

111129
with tempfile.TemporaryDirectory() as csv_dir:
@@ -148,7 +166,7 @@ def test_upload_files_xlsx(mock_client: MagicMock) -> None:
148166
)
149167

150168

151-
def test_upload_files_xlsx_catches_exception(mock_client: MagicMock) -> None:
169+
def test_upload_files_xlsx_catches_upload_exception(mock_client: MagicMock) -> None:
152170
mock_client.upload_file.side_effect = [None, None, Exception("Mock exception")]
153171

154172
with tempfile.TemporaryDirectory() as csv_dir:

0 commit comments

Comments
 (0)