Skip to content

Commit c881dcc

Browse files
authored
Fix More informative error message on import failure (#684)
* Fix More informative error message on import failure Fixes #670 * Handle errors when celery is not configured * Fix test
1 parent fb9a2f8 commit c881dcc

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

gramps_webapi/api/resources/transactions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ def post(self, args) -> ResponseReturnValue:
7070
if isinstance(task, AsyncResult):
7171
return make_task_response(task)
7272
return task, 200
73-
trans_dict = process_transactions(
74-
tree=tree, user_id=user_id, payload=payload, force=args["force"]
75-
)
73+
try:
74+
trans_dict = process_transactions(
75+
tree=tree, user_id=user_id, payload=payload, force=args["force"]
76+
)
77+
except ValueError as exc:
78+
abort_with_message(400, str(exc))
7679
res = Response(
7780
response=json.dumps(trans_dict),
7881
status=200,

gramps_webapi/api/tasks.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from .report import run_report
4848
from .resources.delete import delete_all_objects
4949
from .resources.util import (
50+
abort_with_message,
5051
app_has_semantic_search,
5152
dry_run_import,
5253
run_import,
@@ -60,7 +61,6 @@
6061
update_telemetry_timestamp,
6162
)
6263
from .util import (
63-
abort_with_message,
6464
check_quota_people,
6565
close_db,
6666
get_config,
@@ -76,7 +76,10 @@ def run_task(task: Task, **kwargs) -> Union[AsyncResult, Any]:
7676
"""Send a task to the task queue or run immediately if no queue set up."""
7777
if not current_app.config["CELERY_CONFIG"]:
7878
with current_app.app_context():
79-
return task(**kwargs)
79+
try:
80+
return task(**kwargs)
81+
except Exception as exc:
82+
abort_with_message(500, str(exc))
8083
return task.delay(**kwargs)
8184

8285

@@ -497,7 +500,7 @@ def process_transactions(
497500
):
498501
if num_people_added or num_people_deleted:
499502
update_usage_people(tree=tree, user_id=user_id)
500-
abort_with_message(409, "Object has changed")
503+
raise ValueError("Object has changed")
501504
new_data = item["new"]
502505
if new_data:
503506
new_obj = gramps_object_from_dict(new_data)
@@ -515,11 +518,11 @@ def process_transactions(
515518
else:
516519
if num_people_added or num_people_deleted:
517520
update_usage_people(tree=tree, user_id=user_id)
518-
abort_with_message(400, "Unexpected transaction type")
521+
raise ValueError("Unexpected transaction type")
519522
except (KeyError, UnicodeDecodeError, json.JSONDecodeError, TypeError):
520523
if num_people_added or num_people_deleted:
521524
update_usage_people(tree=tree, user_id=user_id)
522-
abort_with_message(400, "Error while processing transaction")
525+
raise ValueError("Error while processing transaction")
523526
trans_dict = transaction_to_json(trans)
524527
finally:
525528
# close the *writeable* db handle regardless of errors
@@ -570,7 +573,7 @@ def handle_commit(trans: DbTxn, class_name: str, obj) -> None:
570573
def handle_add(trans: DbTxn, class_name: str, obj) -> None:
571574
"""Handle an add action."""
572575
if class_name != "Tag" and not obj.gramps_id:
573-
abort_with_message(400, "Gramps ID missing")
576+
raise ValueError("Gramps ID missing")
574577
handle_commit(trans, class_name, obj)
575578

576579

tests/test_endpoints/test_importers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def test_importers_example_data_quota(self):
203203
data=file_obj,
204204
headers=headers,
205205
)
206-
assert rv.status_code == 405
206+
assert rv.status_code == 500
207207
rv = check_success(self, f"{BASE_URL}/people/")
208208
assert len(rv) == 0
209209
with self.test_app.app_context():

tests/test_endpoints/test_transactions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def test_missing_handle(self):
379379
rv = self.client.post(
380380
"/api/transactions/?background=1", json=trans, headers=headers
381381
)
382-
self.assertEqual(rv.status_code, 400)
382+
self.assertEqual(rv.status_code, 500)
383383

384384
def test_missing_gramps_id(self):
385385
"""Add with missing gramps ID."""
@@ -397,7 +397,7 @@ def test_missing_gramps_id(self):
397397
rv = self.client.post(
398398
"/api/transactions/?background=1", json=trans, headers=headers
399399
)
400-
self.assertEqual(rv.status_code, 400)
400+
self.assertEqual(rv.status_code, 500)
401401

402402
def test_gramps60_issue(self):
403403
"""Test for an issue that occurred after upgrading to Gramps 6.0"""

0 commit comments

Comments
 (0)