# Buggy code
def render_task_status(tasklist):
    renderedtasklist = list()
    for task in tasklist:
        if task["user"] == current_user.nickname or current_user.role_admin():
            if task["formStarttime"]:
                task["starttime"] = format_datetime(
                    task["formStarttime"], format="short", locale=web.get_locale()
                )
            else:
                if "starttime" not in task:
                    task["starttime"] = ""

            task["runtime"] = format_runtime(task["formRuntime"])

            # localize the task status
            if isinstance(task["stat"], int):
                if task["stat"] == worker.STAT_WAITING:
                    task["status"] = _("Waiting")
                elif task["stat"] == worker.STAT_FAIL:
                    task["status"] = _("Failed")
                elif task["stat"] == worker.STAT_STARTED:
                    task["status"] = _("Started")
                elif task["stat"] == worker.STAT_FINISH_SUCCESS:
                    task["status"] = _("Finished")
                else:
                    task["status"] = _("Unknown Status")

            # localize the task type
            if isinstance(task["taskType"], int):
                if task["taskType"] == worker.TASK_EMAIL:
                    task["taskMessage"] = _("E-mail: ") + task["taskMess"]
                elif task["taskType"] == worker.TASK_CONVERT:
                    task["taskMessage"] = _("Convert: ") + task["taskMess"]
                elif task["taskType"] == worker.TASK_UPLOAD:
                    task["taskMessage"] = _("Upload: ") + task["taskMess"]
                elif task["taskType"] == worker.TASK_CONVERT_ANY:
                    task["taskMessage"] = _("Convert: ") + task["taskMess"]
                else:
                    task["taskMessage"] = _("Unknown Task: ") + task["taskMess"]

            renderedtasklist.append(task)

    return renderedtasklist


# Traceback (KeyError)
Jul 11 20:01:27 lara python3[24820]: [2019-07-11 20:01:27,270] INFO in worker: Book id 42929 - target format of .mobi does not exist. Moving forward with convert.
Jul 11 20:01:27 lara python3[24820]: [2019-07-11 20:01:27,784] DEBUG in worker: 1% Converting input to HTML...
Jul 11 20:01:27 lara python3[24820]: [2019-07-11 20:01:27,784] DEBUG in worker: InputFormatPlugin: EPUB Input running
Jul 11 20:01:27 lara python3[24820]: [2019-07-11 20:01:27,785] DEBUG in worker: on /sharedfolders/ebooks/Shamash Alidina/Mindfulness voor Dummies (42929)/Mindfulness voor Dummies - Shamash Alidina.epub
Jul 11 20:01:28 lara python3[24820]: [2019-07-11 20:01:28,047] DEBUG in worker: Found HTML cover OEBPS/Text/Section0001.html
Jul 11 20:01:28 lara python3[24820]: [2019-07-11 20:01:28,064] DEBUG in worker: Parsing all content...
Jul 11 20:01:28 lara python3[24820]: [2019-07-11 20:01:28,915] DEBUG in worker: 34% Running transforms on e-book...
Jul 11 20:01:29 lara python3[24820]: [2019-07-11 20:01:29,100] DEBUG in worker: Merging user specified metadata...
Jul 11 20:01:29 lara python3[24820]: [2019-07-11 20:01:29,101] DEBUG in worker: Detecting structure...
Jul 11 20:01:29 lara python3[24820]: [2019-07-11 20:01:29,173] DEBUG in worker: Flattening CSS and remapping font sizes...
Jul 11 20:01:33 lara python3[24820]: [2019-07-11 20:01:33,266] DEBUG in worker: Source base font size is 12.00000pt
Jul 11 20:01:34 lara python3[24820]: [2019-07-11 20:01:34,651] DEBUG in worker: Removing fake margins...
Jul 11 20:01:35 lara python3[24820]: [2019-07-11 20:01:35,409] DEBUG in worker: Cleaning up manifest...
Jul 11 20:01:35 lara python3[24820]: [2019-07-11 20:01:35,410] DEBUG in worker: Trimming unused files from manifest...
Jul 11 20:01:35 lara python3[24820]: [2019-07-11 20:01:35,475] DEBUG in worker: Trimming u'OEBPS/Text/Section0001.html' from manifest
Jul 11 20:01:35 lara python3[24820]: [2019-07-11 20:01:35,601] DEBUG in worker: Creating MOBI Output...
Jul 11 20:01:35 lara python3[24820]: [2019-07-11 20:01:35,602] DEBUG in worker: 67% Running MOBI Output plugin
Jul 11 20:01:35 lara python3[24820]: [2019-07-11 20:01:35,722] DEBUG in worker: Serializing resources...
Jul 11 20:01:36 lara python3[24820]: [2019-07-11 20:01:36,594] DEBUG in worker: Creating MOBI 6 output
Jul 11 20:01:36 lara python3[24820]: [2019-07-11 20:01:36,682] DEBUG in worker: Generating in-line TOC...
Jul 11 20:01:36 lara python3[24820]: [2019-07-11 20:01:36,693] DEBUG in worker: Applying case-transforming CSS...
Jul 11 20:01:39 lara systemd[1]: Started Session 3268 of user root.
Jul 11 20:01:44 lara python3[24820]: [2019-07-11 20:01:44,777] DEBUG in worker: Rasterizing SVG images...
Jul 11 20:01:44 lara python3[24820]: [2019-07-11 20:01:44,810] DEBUG in worker: Converting XHTML to Mobipocket markup...
Jul 11 20:01:57 lara python3[24820]: [2019-07-11 20:01:57,970] DEBUG in worker: Serializing markup content...
Jul 11 20:01:58 lara python3[24820]: [2019-07-11 20:01:58,585] DEBUG in worker:   Compressing markup content...
Jul 11 20:02:01 lara python3[24820]: [2019-07-11 20:02:01,547] ERROR in app: Exception on /tasks [GET]
Jul 11 20:02:01 lara python3[24820]: Traceback (most recent call last):
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/vendor/flask/app.py", line 2311, in wsgi_app
Jul 11 20:02:01 lara python3[24820]:     response = self.full_dispatch_request()
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/vendor/flask/app.py", line 1834, in full_dispatch_request
Jul 11 20:02:01 lara python3[24820]:     rv = self.handle_user_exception(e)
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/vendor/flask/app.py", line 1737, in handle_user_exception
Jul 11 20:02:01 lara python3[24820]:     reraise(exc_type, exc_value, tb)
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/vendor/flask/_compat.py", line 36, in reraise
Jul 11 20:02:01 lara python3[24820]:     raise value
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/vendor/flask/app.py", line 1832, in full_dispatch_request
Jul 11 20:02:01 lara python3[24820]:     rv = self.dispatch_request()
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/vendor/flask/app.py", line 1818, in dispatch_request
Jul 11 20:02:01 lara python3[24820]:     return self.view_functions[rule.endpoint](**req.view_args)
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/vendor/flask_login/utils.py", line 261, in decorated_view
Jul 11 20:02:01 lara python3[24820]:     return func(*args, **kwargs)
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/cps/web.py", line 1647, in get_tasks_status
Jul 11 20:02:01 lara python3[24820]:     answer = helper.render_task_status(tasks)
Jul 11 20:02:01 lara python3[24820]:   File "/home/calibre-web/calibre-web/cps/helper.py", line 608, in render_task_status
Jul 11 20:02:01 lara python3[24820]:     task['runtime'] = format_runtime(task['formRuntime'])
Jul 11 20:02:01 lara python3[24820]: KeyError: 'formRuntime'
Jul 11 20:02:04 lara python3[24820]: ::ffff:192.168.100.156 - - [2019-07-11 20:02:01] "GET /tasks HTTP/1.1" 500 411 0.013245

# Fixed code
def render_task_status(tasklist):
    renderedtasklist = list()
    for task in tasklist:
        if task["user"] == current_user.nickname or current_user.role_admin():
            if task["formStarttime"]:
                task["starttime"] = format_datetime(
                    task["formStarttime"], format="short", locale=web.get_locale()
                )
            else:
                if "starttime" not in task:
                    task["starttime"] = ""

            if "formRuntime" not in task:
                task["runtime"] = ""
            else:
                task["runtime"] = format_runtime(task["formRuntime"])

            # localize the task status
            if isinstance(task["stat"], int):
                if task["stat"] == worker.STAT_WAITING:
                    task["status"] = _("Waiting")
                elif task["stat"] == worker.STAT_FAIL:
                    task["status"] = _("Failed")
                elif task["stat"] == worker.STAT_STARTED:
                    task["status"] = _("Started")
                elif task["stat"] == worker.STAT_FINISH_SUCCESS:
                    task["status"] = _("Finished")
                else:
                    task["status"] = _("Unknown Status")

            # localize the task type
            if isinstance(task["taskType"], int):
                if task["taskType"] == worker.TASK_EMAIL:
                    task["taskMessage"] = _("E-mail: ") + task["taskMess"]
                elif task["taskType"] == worker.TASK_CONVERT:
                    task["taskMessage"] = _("Convert: ") + task["taskMess"]
                elif task["taskType"] == worker.TASK_UPLOAD:
                    task["taskMessage"] = _("Upload: ") + task["taskMess"]
                elif task["taskType"] == worker.TASK_CONVERT_ANY:
                    task["taskMessage"] = _("Convert: ") + task["taskMess"]
                else:
                    task["taskMessage"] = _("Unknown Task: ") + task["taskMess"]

            renderedtasklist.append(task)

    return renderedtasklist