From 184084c6a86ed59d7177477078ff826953fc489f Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Mon, 25 Mar 2019 07:45:26 -0700 Subject: [PATCH] Prevent occasional crash during task re-ordering Prevent occasional crash during task re-ordering --- todo/views/reorder_tasks.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/todo/views/reorder_tasks.py b/todo/views/reorder_tasks.py index 843a086..04e2c1b 100644 --- a/todo/views/reorder_tasks.py +++ b/todo/views/reorder_tasks.py @@ -20,10 +20,15 @@ def reorder_tasks(request) -> HttpResponse: # Re-prioritize each task in list i = 1 for id in newtasklist: - task = Task.objects.get(pk=id) - task.priority = i - task.save() - i += 1 + try: + task = Task.objects.get(pk=id) + task.priority = i + task.save() + i += 1 + except Task.DoesNotExist: + # Can occur if task is deleted behind the scenes during re-ordering. + # Not easy to remove it from the UI without page refresh, but prevent crash. + pass # All views must return an httpresponse of some kind ... without this we get # error 500s in the log even though things look peachy in the browser.