Skip to content

Raise FileNotFoundError instead of panic when opening missing files #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions python/src/tiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use async_tiff::metadata::{PrefetchBuffer, TiffMetadataReader};
use async_tiff::reader::AsyncFileReader;
use async_tiff::TIFF;
use pyo3::exceptions::PyIndexError;
use pyo3::exceptions::{PyFileNotFoundError, PyIndexError};
use pyo3::prelude::*;
use pyo3::types::PyType;
use pyo3_async_runtimes::tokio::future_into_py;
Expand Down Expand Up @@ -32,7 +32,9 @@ impl PyTIFF {
let reader = store.into_async_file_reader(path);

let cog_reader = future_into_py(py, async move {
let metadata_fetch = PrefetchBuffer::new(reader.clone(), prefetch).await.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should remove all of these unwraps, but we can leave that for the future.

let metadata_fetch = PrefetchBuffer::new(reader.clone(), prefetch)
.await
.map_err(|err| PyFileNotFoundError::new_err(err.to_string()))?;
let mut metadata_reader = TiffMetadataReader::try_open(&metadata_fetch).await.unwrap();
let ifds = metadata_reader
.read_all_ifds(&metadata_fetch)
Expand Down
13 changes: 12 additions & 1 deletion python/tests/test_cog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from async_tiff import TIFF, enums
from async_tiff.store import S3Store
from async_tiff.store import LocalStore, S3Store


async def test_cog_s3():
Expand All @@ -23,3 +25,12 @@ async def test_cog_s3():
gkd = ifd.geo_key_directory
assert gkd.citation == "WGS 84 / UTM zone 12N"
assert gkd.projected_type == 32612


async def test_cog_missing_file():
"""
Ensure that a FileNotFoundError is raised when passing in a missing file.
"""
store = LocalStore()
with pytest.raises(FileNotFoundError):
await TIFF.open(path="imaginary_file.tif", store=store)