Skip to content

Commit

Permalink
better error message when colormap parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Mar 26, 2021
1 parent 3a6e5c5 commit 588bfc4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
12 changes: 12 additions & 0 deletions tests/routes/test_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ def test_tile(rio, app):
assert response.status_code == 200
assert response.headers["content-type"] == "image/png"

cmap = urlencode({"colormap": json.dumps({"1": [58, 102]})})
response = app.get(
f"/cog/tiles/8/53/50.png?url=https://myurl.com/above_cog.tif&bidx=1&{cmap}"
)
assert response.status_code == 400

cmap = urlencode({"colormap": {"1": "#ddcb9aFF"}})
response = app.get(
f"/cog/tiles/8/53/50.png?url=https://myurl.com/above_cog.tif&bidx=1&{cmap}"
)
assert response.status_code == 400

response = app.get(
"/cog/tiles/8/53/50.png?url=https://myurl.com/above_cog.tif&bidx=1&colormap_name=above&resampling_method=somethingwrong"
)
Expand Down
15 changes: 10 additions & 5 deletions titiler/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .custom import tms as custom_tms
from .utils import get_hash

from fastapi import Query
from fastapi import HTTPException, Query

from starlette.requests import Request

Expand Down Expand Up @@ -78,10 +78,15 @@ def ColorMapParams(
return cmap.get(colormap_name.value)

if colormap:
return json.loads(
colormap,
object_hook=lambda x: {int(k): parse_color(v) for k, v in x.items()},
)
try:
return json.loads(
colormap,
object_hook=lambda x: {int(k): parse_color(v) for k, v in x.items()},
)
except json.JSONDecodeError:
raise HTTPException(
status_code=400, detail="Could not parse the colormap value."
)

return None

Expand Down
2 changes: 2 additions & 0 deletions titiler/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
EmptyMosaicError,
InvalidAssetName,
InvalidBandName,
InvalidColorFormat,
MissingAssets,
MissingBands,
RioTilerError,
Expand Down Expand Up @@ -52,6 +53,7 @@ class BadRequestError(TilerError):
RasterioIOError: status.HTTP_404_NOT_FOUND,
MissingBands: status.HTTP_400_BAD_REQUEST,
MissingAssets: status.HTTP_400_BAD_REQUEST,
InvalidColorFormat: status.HTTP_400_BAD_REQUEST,
InvalidAssetName: status.HTTP_404_NOT_FOUND,
InvalidBandName: status.HTTP_404_NOT_FOUND,
MosaicError: status.HTTP_424_FAILED_DEPENDENCY,
Expand Down

0 comments on commit 588bfc4

Please sign in to comment.