Skip to content

Commit 873999e

Browse files
committed
Better exception management for corrupted/missing files
1 parent 3768d56 commit 873999e

3 files changed

Lines changed: 15 additions & 21 deletions

File tree

eoreader/products/optical/dimap_v2_product.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -974,37 +974,29 @@ def open_mask(self, mask_str: str, **kwargs) -> gpd.GeoDataFrame:
974974
# Empty mask cannot be written on file
975975
mask = gpd.GeoDataFrame(geometry=[], crs=crs)
976976
else:
977-
if self.is_archived:
978-
# Open the zip file
979-
try:
977+
try:
978+
if self.is_archived:
979+
# Open the zip file
980980
mask = vectors.read(
981981
self.path,
982982
archive_regex=rf".*MASKS.*{mask_str}.*\.GML",
983983
crs=crs,
984984
)
985-
except Exception:
986-
if mask_str in optional_masks:
987-
mask = gpd.GeoDataFrame(geometry=[], crs=crs)
988-
else:
989-
raise InvalidProductError(
990-
f"Mask {mask_str} not found for {self.path}"
991-
)
992-
else:
993-
try:
985+
else:
994986
mask_gml_path = files.get_file_in_dir(
995987
self.path.joinpath("MASKS"),
996988
f"*{mask_str}*.GML",
997989
exact_name=True,
998990
)
999991

1000992
mask = vectors.read(mask_gml_path, crs=crs)
1001-
except FileNotFoundError:
1002-
if mask_str in optional_masks:
1003-
mask = gpd.GeoDataFrame(geometry=[], crs=crs)
1004-
else:
1005-
raise InvalidProductError(
1006-
f"Mask {mask_str} not found for {self.path}"
1007-
)
993+
except FileNotFoundError:
994+
if mask_str in optional_masks:
995+
mask = gpd.GeoDataFrame(geometry=[], crs=crs)
996+
else:
997+
raise InvalidProductError(
998+
f"Mask {mask_str} not found for {self.path.joinpath('MASKS')}"
999+
)
10081000

10091001
# Convert mask to correct CRS
10101002
if not mask.empty and self.product_type in [

eoreader/products/optical/s2_product.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,9 @@ def _read_band(
679679
out_ds.transform = tf
680680

681681
except errors.RasterioIOError as ex:
682-
if str(path).endswith("jp2") or str(path).endswith("tif"):
682+
if (
683+
str(path).endswith("jp2") or str(path).endswith("tif")
684+
) and path.exists():
683685
raise InvalidProductError(f"Corrupted file: {path}") from ex
684686
else:
685687
raise ex

eoreader/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def read(
180180
**_prune_keywords(additional_keywords=["window"], **kwargs),
181181
)
182182
except errors.RasterioIOError as ex:
183-
if str(path).endswith("jp2") or str(path).endswith("tif"):
183+
if (str(path).endswith("jp2") or str(path).endswith("tif")) and path.exists():
184184
raise InvalidProductError(f"Corrupted file: {path}") from ex
185185
else:
186186
raise

0 commit comments

Comments
 (0)