Repository: kevoreilly/CAPEv2

Issue:
APIv2 crashes in tasks_status and tasks_view when tasks don't exist
## About accounts on [capesandbox.com](https://capesandbox.com/)
* Issues isn't the way to ask for account acctivation. Ping capesandbox in [Twitter](https://twitter.com/capesandbox) with your username

## This is opensource and you getting __free__ support so be friendly!
* Free support from doomedraven ended, no whiskey no support. For something he updated the documentation :)

# Prerequisites

Please answer the following questions for yourself before submitting an issue.

- [x] I am running the latest version
- [x] I checked the documentation and found no answer
- [x] I checked to make sure that this issue has not already been filed
- [x] I'm reporting the issue to the correct repository (for multi-repository projects)
- [x] I'm have read all configs with all optional parts


# Expected Behavior

Please describe the behavior you are expecting. __If your samples(x64) stuck in pending ensure that you set tags=x64 in hypervisor conf for x64 vms__

# Current Behavior

APIv2 crashes when missing task ids are given to the following routes:
- tasks/view/<task_id>
- tasks/status/<task_id>

# Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

## Steps to Reproduce

Give invalid task ids to either route.


Discussion:

Resolving patch:
diff --git a/web/apiv2/views.py b/web/apiv2/views.py
--- a/web/apiv2/views.py
+++ b/web/apiv2/views.py
@@ -848,25 +848,27 @@ def tasks_view(request, task_id):
         resp = {"error": True, "error_value": "Task View API is Disabled"}
         return Response(resp)
 
-    resp = {}
     task = db.view_task(task_id, details=True)
-    resp["error"] = False
-    if task:
-        entry = task.to_dict()
-        if entry["category"] != "url":
-            entry["target"] = entry["target"].rsplit("/", 1)[-1]
-        entry["guest"] = {}
-        if task.guest:
-            entry["guest"] = task.guest.to_dict()
-
-        entry["errors"] = []
-        for error in task.errors:
-            entry["errors"].append(error.message)
-
-        entry["sample"] = {}
-        if task.sample_id:
-            sample = db.view_sample(task.sample_id)
-            entry["sample"] = sample.to_dict()
+    if not task:
+        resp = {"error": True, "error_value": "Task not found in database"}
+        return Response(resp)
+
+    resp = {"error": False}
+    entry = task.to_dict()
+    if entry["category"] != "url":
+        entry["target"] = entry["target"].rsplit("/", 1)[-1]
+    entry["guest"] = {}
+    if task.guest:
+        entry["guest"] = task.guest.to_dict()
+
+    entry["errors"] = []
+    for error in task.errors:
+        entry["errors"].append(error.message)
+
+    entry["sample"] = {}
+    if task.sample_id:
+        sample = db.view_sample(task.sample_id)
+        entry["sample"] = sample.to_dict()
 
     if repconf.mongodb.enabled:
         rtmp = mongo_find_one(
@@ -1038,12 +1040,13 @@ def tasks_status(request, task_id):
         resp = {"error": True, "error_value": "Task status API is disabled"}
         return Response(resp)
 
-    status = db.view_task(task_id).to_dict()["status"]
-    if not status:
+    task = db.view_task(task_id)
+    if not task:
         resp = {"error": True, "error_value": "Task does not exist"}
-    else:
-        resp = {"error": False, "data": status}
+        return Response(resp)
 
+    status = task.to_dict()["status"]
+    resp = {"error": False, "data": status}
     return Response(resp)
 
 


Test patch: