Skip to content

Commit 5d74ca5

Browse files
committed
Fix: Add error handling for missing 'tiles' key in stac_tile response
When TiTiler endpoint returns an error response (e.g., validation errors), the response doesn't contain a 'tiles' key, causing a KeyError. This fix: - Checks if 'tiles' exists in the response before accessing it - Provides informative error messages based on the API error response - Handles both dict-based errors (with 'detail' key) and list-based validation errors - Raises ValueError with descriptive message instead of bare KeyError Fixes opengeos/geoai#468
1 parent cf92c4e commit 5d74ca5

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

leafmap/stac.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,26 @@ def stac_tile(
850850
titiler_endpoint.url_for_stac_item(), params=kwargs, timeout=10
851851
).json()
852852

853+
# Check if the response contains tiles
854+
if "tiles" not in r:
855+
# Try to provide helpful error message from the API response
856+
if "detail" in r:
857+
error_msg = f"TiTiler endpoint error: {r['detail']}"
858+
elif isinstance(r, list) and len(r) > 0:
859+
# Handle validation errors (list of error dicts)
860+
if isinstance(r[0], dict) and "msg" in r[0]:
861+
errors = []
862+
for e in r:
863+
loc = "->".join(str(x) for x in e.get("loc", []))
864+
msg = e.get("msg", "")
865+
errors.append(f"{loc}: {msg}")
866+
error_msg = f"TiTiler endpoint validation failed: {'; '.join(errors)}"
867+
else:
868+
error_msg = f"TiTiler endpoint returned error list: {r}"
869+
else:
870+
error_msg = f"TiTiler endpoint returned unexpected response (missing 'tiles' key): {r}"
871+
raise ValueError(error_msg)
872+
853873
tiles = r["tiles"][0]
854874

855875
# Convert 127.0.0.1 to localhost for Google Colab browser access

0 commit comments

Comments
 (0)