Skip to content

Commit ebcb497

Browse files
committed
Fix: Prevent titiler_endpoint from being overwritten in stac_tile
When stac_stats fails for Planetary Computer items, it was overwriting the titiler_endpoint variable with a fallback string endpoint. This caused the subsequent tile request to use the wrong endpoint path (/stac/ instead of /item/), which expects different parameters and returns errors. Changes: 1. Use a separate fallback_endpoint variable for stac_stats fallback 2. Add error handling to check for 'tiles' key before accessing it 3. Provide informative error messages based on API response type Fixes opengeos/geoai#468
1 parent cf92c4e commit ebcb497

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

leafmap/stac.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,12 @@ def stac_tile(
706706
titiler_endpoint=titiler_endpoint,
707707
)
708708
except Exception as e:
709-
titiler_endpoint = "https://giswqs-titiler-endpoint.hf.space"
709+
fallback_endpoint = "https://giswqs-titiler-endpoint.hf.space"
710710
stats = stac_stats(
711711
collection=collection,
712712
item=item,
713713
assets=assets,
714-
titiler_endpoint=titiler_endpoint,
714+
titiler_endpoint=fallback_endpoint,
715715
)
716716

717717
if "detail" not in stats:
@@ -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)