Skip to content
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

Better colormap parsing error #279

Merged
merged 2 commits into from
Mar 26, 2021
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next (TBD)

* add support for `.jpg` and `.jpeg` extensions (https://github.com/developmentseed/titiler/pull/271)
* better error message when parsing the colormap value fails (https://github.com/developmentseed/titiler/pull/279)

## 0.2.0 (2021-03-09)

Expand Down
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