Skip to content

Commit 0f50478

Browse files
committed
Return specific API error when requested block type does not exist
1 parent db3daa3 commit 0f50478

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

pydatalab/src/pydatalab/routes/v0_1/blocks.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask import Blueprint, jsonify, request
2-
from werkzeug.exceptions import BadRequest
2+
from werkzeug.exceptions import BadRequest, NotImplemented
33

44
from pydatalab.apps import BLOCK_TYPES
55
from pydatalab.blocks.base import DataBlock
@@ -26,12 +26,8 @@ def add_data_block():
2626
insert_index = request_json["index"]
2727

2828
if block_type not in BLOCK_TYPES:
29-
return (
30-
jsonify(
31-
status="error",
32-
message=f"Invalid block type {block_type!r}, must be one of {BLOCK_TYPES.keys()}",
33-
),
34-
400,
29+
raise NotImplemented( # noqa
30+
f"Invalid block type {block_type!r}, must be one of {BLOCK_TYPES.keys()}"
3531
)
3632

3733
block = BLOCK_TYPES[block_type](item_id=item_id)
@@ -92,7 +88,9 @@ def add_collection_data_block():
9288
insert_index = request_json["index"]
9389

9490
if block_type not in BLOCK_TYPES:
95-
return jsonify(status="error", message="Invalid block type"), 400
91+
raise NotImplemented( # noqa
92+
f"Invalid block type {block_type!r}, must be one of {BLOCK_TYPES.keys()}"
93+
)
9694

9795
block = BLOCK_TYPES[block_type](collection_id=collection_id)
9896

@@ -169,7 +167,7 @@ def _save_block_to_db(block: DataBlock) -> None:
169167

170168
if result.matched_count != 1:
171169
raise BadRequest(
172-
f"_Failed to save block, likely because item_id ({block.data.get('item_id')}), collection_id ({block.data.get('collection_id')}) and/or block_id ({block.block_id}) wasn't found"
170+
f"_save_block_to_db failed, likely because item_id ({block.data.get('item_id')}), collection_id ({block.data.get('collection_id')}) and/or block_id ({block.block_id}) wasn't found"
173171
)
174172

175173

@@ -183,9 +181,14 @@ def update_block():
183181
request_json = request.get_json()
184182
block_data = request_json["block_data"]
185183
event_data = request_json.get("event_data", None)
186-
blocktype = block_data["blocktype"]
184+
block_type = block_data["blocktype"]
185+
186+
if block_type not in BLOCK_TYPES:
187+
raise NotImplemented( # noqa
188+
f"Invalid block type {block_type!r}, must be one of {BLOCK_TYPES.keys()}"
189+
)
187190

188-
block = BLOCK_TYPES[blocktype].from_web(block_data)
191+
block = BLOCK_TYPES[block_type].from_web(block_data)
189192

190193
if event_data:
191194
try:

0 commit comments

Comments
 (0)