# Buggy code
def take_screenshot(request):
    """Take Screenshot"""
    logger.info("Taking Screenshot")
    try:
        if request.method == "POST":
            md5_hash = request.POST["md5"]
            if re.match("^[0-9a-f]{32}$", md5_hash):
                data = {}
                rand_int = random.randint(1, 1000000)
                base_dir = settings.BASE_DIR
                # make sure that list only png from this directory
                screen_dir = os.path.join(
                    settings.UPLD_DIR, md5_hash + "/screenshots-apk/"
                )
                if not os.path.exists(screen_dir):
                    os.makedirs(screen_dir)
                adb_command(["screencap", "-p", "/data/local/screen.png"], True)
                adb_command(
                    [
                        "pull",
                        "/data/local/screen.png",
                        screen_dir + "screenshot-" + str(rand_int) + ".png",
                    ]
                )
                logger.info("Screenshot Taken")
                data = {"screenshot": "yes"}
                return HttpResponse(json.dumps(data), content_type="application/json")
            else:
                return HttpResponseRedirect("/error/")
        else:
            return HttpResponseRedirect("/error/")
    except:
        PrintException("[ERROR] Taking Screenshot")
        return HttpResponseRedirect("/error/")


# Traceback (KeyError)
[INFO] 08/Feb/2019 16:17:54 - VM Starting
[INFO] 08/Feb/2019 16:17:55 - "POST /DynamicAnalyzer/ HTTP/1.1" 200 32400
[INFO] 08/Feb/2019 16:17:55 - "GET /static/css/devices.min.css HTTP/1.1" 200 44241
[INFO] 08/Feb/2019 16:17:55 - "GET /download/screen/screen.png HTTP/1.1" 302 0
[DEBUG] 08/Feb/2019 16:17:55 - Exception while resolving variable 'exp' in template 'general/error.html'.
Traceback (most recent call last):
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/base.py", line 829, in _resolve_lookup
current = current[bit]
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/context.py", line 83, in __getitem__
raise KeyError(key)
KeyError: 'exp'

During handling of the above exception, another exception occurred:

# Fixed code
def take_screenshot(request):
    """Take Screenshot"""
    logger.info("Taking Screenshot")
    try:
        if request.method == "POST":
            md5_hash = request.POST["md5"]
            if re.match("^[0-9a-f]{32}$", md5_hash):
                data = {}
                rand_int = random.randint(1, 1000000)
                base_dir = settings.BASE_DIR
                # make sure that list only png from this directory
                screen_dir = os.path.join(
                    settings.UPLD_DIR, md5_hash + "/screenshots-apk/"
                )
                if not os.path.exists(screen_dir):
                    os.makedirs(screen_dir)
                adb_command(["screencap", "-p", "/data/local/screen.png"], True)
                adb_command(
                    [
                        "pull",
                        "/data/local/screen.png",
                        screen_dir + "screenshot-" + str(rand_int) + ".png",
                    ]
                )
                logger.info("Screenshot Taken")
                data = {"screenshot": "yes"}
                return HttpResponse(json.dumps(data), content_type="application/json")
            else:
                return print_n_send_error_response(request, "Invalid Scan Hash", True)
        else:
            return print_n_send_error_response(request, "Only POST allowed", True)
    except:
        PrintException("Taking Screenshot")
        return print_n_send_error_response(request, "Error Taking Screenshot", True)