diff --git a/python/src/tiff.rs b/python/src/tiff.rs index d173aa8..f88e8e8 100644 --- a/python/src/tiff.rs +++ b/python/src/tiff.rs @@ -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::{PyFileNotFoundError, PyIndexError}; +use pyo3::exceptions::{PyFileNotFoundError, PyIndexError, PyTypeError}; use pyo3::prelude::*; use pyo3::types::PyType; use pyo3_async_runtimes::tokio::future_into_py; @@ -69,7 +69,11 @@ impl PyTIFF { // TODO: avoid this clone; add Arc to underlying rust code? .clone(); future_into_py(py, async move { - let tile = ifd.fetch_tile(x, y, reader.as_ref()).await.unwrap(); + let tile = ifd + .fetch_tile(x, y, reader.as_ref()) + .await + .map_err(|err| PyTypeError::new_err(err.to_string()))?; + Ok(PyTile::new(tile)) }) } @@ -91,7 +95,10 @@ impl PyTIFF { // TODO: avoid this clone; add Arc to underlying rust code? .clone(); future_into_py(py, async move { - let tiles = ifd.fetch_tiles(&x, &y, reader.as_ref()).await.unwrap(); + let tiles = ifd + .fetch_tiles(&x, &y, reader.as_ref()) + .await + .map_err(|err| PyTypeError::new_err(err.to_string()))?; let py_tiles = tiles.into_iter().map(PyTile::new).collect::>(); Ok(py_tiles) }) diff --git a/python/tests/test_exceptions.py b/python/tests/test_exceptions.py new file mode 100644 index 0000000..23137f3 --- /dev/null +++ b/python/tests/test_exceptions.py @@ -0,0 +1,22 @@ +""" +Unit tests to ensure that proper errors are raised instead of a panic. +""" + +import pytest +from async_tiff.store import HTTPStore + +from async_tiff import TIFF + + +async def test_raise_typeerror_fetch_tile_striped_tiff(): + """ + Ensure that a TypeError is raised when trying to fetch a tile from a striped TIFF. + """ + store = HTTPStore(url="https://github.com/") + path = "OSGeo/gdal/raw/refs/tags/v3.11.0/autotest/gdrivers/data/gtiff/int8.tif" + + tiff = await TIFF.open(path=path, store=store) + assert len(tiff.ifds) >= 1 + + with pytest.raises(TypeError): + await tiff.fetch_tile(0, 0, 0)