prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Getting random scan for non-existing task key results in 500
  ## Current Behavior
  
  Providing non existing task key results in 500 HTTP code.
  
  ## Expected Behavior
  
  Backend should handle this situation appropriate and return 404 HTTP code.
  
  ## Steps to Reproduce the Problem
  
    1. Perform a GET `scans/random?task=<task_key>` and provide non existing key.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `backend/medtagger/repositories/tasks.py`
  Content:
  ```
  1 """Module responsible for definition of TaskRepository."""
  2 from typing import List
  3 
  4 from medtagger.database import db_session
  5 from medtagger.database.models import Task, LabelTag, Dataset
  6 from medtagger.exceptions import InternalErrorException
  7 
  8 
  9 def get_all_tasks(include_disabled: bool = False) -> List[Task]:
  10     """Fetch all tasks from database ordered by key."""
  11     query = Task.query
  12     if not include_disabled:
  13         query = query.filter(~Task.disabled)
  14     return query.order_by(Task.key).all()
  15 
  16 
  17 def get_task_by_key(key: str) -> Task:
  18     """Fetch Task from database.
  19 
  20     :param key: key for a Task
  21     :return: Task object
  22     """
  23     with db_session() as session:
  24         task = session.query(Task).filter(Task.key == key).one()
  25     return task
  26 
  27 
  28 def add_task(key: str, name: str, image_path: str, datasets_keys: List[str], tags: List[LabelTag]) -> Task:
  29     """Add new Task to the database.
  30 
  31     :param key: key that will identify such Task
  32     :param name: name that will be used in the Use Interface for such Task
  33     :param image_path: path to the image that represents such Task (used in User Interface)
  34     :param datasets_keys: Keys of Datasets that Task takes Scans from
  35     :param tags: Label Tags that will be created and assigned to Task
  36     :return: Task object
  37     """
  38     with db_session() as session:
  39         task = Task(key, name, image_path)
  40         datasets = Dataset.query.filter(Dataset.key.in_(datasets_keys)).all()  # type: ignore
  41         task.datasets = datasets
  42         task.available_tags = tags
  43         session.add(task)
  44     return task
  45 
  46 
  47 def assign_label_tag(tag: LabelTag, task_key: str) -> None:
  48     """Assign existing Label Tag to Task.
  49 
  50     :param tag: tag that should be assigned to Task
  51     :param task_key: key that will identify such Task
  52     """
  53     with db_session():
  54         task = Task.query.filter(Task.key == task_key).one()
  55         task.available_tags.append(tag)
  56         task.save()
  57 
  58 
  59 def unassign_label_tag(tag: LabelTag, task_key: str) -> None:
  60     """Unassign Label Tag from Task.
  61 
  62     :param tag: tag that should be unassigned from Task
  63     :param task_key: key that will identify such Task
  64     """
  65     with db_session():
  66         task = Task.query.filter(Task.key == task_key).one()
  67         task.available_tags.remove(tag)
  68         task.save()
  69 
  70 
  71 def update(task_key: str, name: str = None, image_path: str = None, datasets_keys: List[str] = None) -> Task:
  72     """Update Datasets where this Task will be available.
  73 
  74     :param task_key: key that will identify such Task
  75     :param name: (optional) new name for such Task
  76     :param image_path: (optional) new path to the image which shows on the UI
  77     :param datasets_keys: (optional) keys of Datasets which should have this Task
  78     """
  79     with db_session():
  80         task = Task.query.filter(Task.key == task_key).one()
  81         if name:
  82             task.name = name
  83         if image_path:
  84             task.image_path = image_path
  85         if datasets_keys:
  86             datasets = Dataset.query.filter(Dataset.key.in_(datasets_keys)).all()  # type: ignore
  87             task.datasets = datasets
  88     return task
  89 
  90 
  91 def disable(task_key: str) -> None:
  92     """Disable existing Task."""
  93     disabling_query = Task.query.filter(Task.key == task_key)
  94     updated = disabling_query.update({'disabled': True}, synchronize_session='fetch')
  95     if not updated:
  96         raise InternalErrorException(f'Task "{task_key}" was not disabled due to unknown database error.')
  97 
  98 
  99 def enable(task_key: str) -> None:
  100     """Enable existing Task."""
  101     enabling_query = Task.query.filter(Task.key == task_key)
  102     updated = enabling_query.update({'disabled': False}, synchronize_session='fetch')
  103     if not updated:
  104         raise InternalErrorException(f'Task "{task_key}" was not enabled due to unknown database error.')
  105
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/backend/medtagger/repositories/tasks.py b/backend/medtagger/repositories/tasks.py
  --- a/backend/medtagger/repositories/tasks.py
  +++ b/backend/medtagger/repositories/tasks.py
  @@ -21,7 +21,7 @@
       :return: Task object
       """
       with db_session() as session:
  -        task = session.query(Task).filter(Task.key == key).one()
  +        task = session.query(Task).filter(Task.key == key).first()
       return task
  
verification_info: {"golden_diff": "diff --git a/backend/medtagger/repositories/tasks.py b/backend/medtagger/repositories/tasks.py\n--- a/backend/medtagger/repositories/tasks.py\n+++ b/backend/medtagger/repositories/tasks.py\n@@ -21,7 +21,7 @@\n     :return: Task object\n     \"\"\"\n     with db_session() as session:\n-        task = session.query(Task).filter(Task.key == key).one()\n+        task = session.query(Task).filter(Task.key == key).first()\n     return task\n", "issue": "Getting random scan for non-existing task key results in 500\n## Current Behavior\r\n\r\nProviding non existing task key results in 500 HTTP code.\r\n\r\n## Expected Behavior\r\n\r\nBackend should handle this situation appropriate and return 404 HTTP code.\r\n\r\n## Steps to Reproduce the Problem\r\n\r\n  1. Perform a GET `scans/random?task=<task_key>` and provide non existing key.\r\n\n", "before_files": [{"content": "\"\"\"Module responsible for definition of TaskRepository.\"\"\"\nfrom typing import List\n\nfrom medtagger.database import db_session\nfrom medtagger.database.models import Task, LabelTag, Dataset\nfrom medtagger.exceptions import InternalErrorException\n\n\ndef get_all_tasks(include_disabled: bool = False) -> List[Task]:\n    \"\"\"Fetch all tasks from database ordered by key.\"\"\"\n    query = Task.query\n    if not include_disabled:\n        query = query.filter(~Task.disabled)\n    return query.order_by(Task.key).all()\n\n\ndef get_task_by_key(key: str) -> Task:\n    \"\"\"Fetch Task from database.\n\n    :param key: key for a Task\n    :return: Task object\n    \"\"\"\n    with db_session() as session:\n        task = session.query(Task).filter(Task.key == key).one()\n    return task\n\n\ndef add_task(key: str, name: str, image_path: str, datasets_keys: List[str], tags: List[LabelTag]) -> Task:\n    \"\"\"Add new Task to the database.\n\n    :param key: key that will identify such Task\n    :param name: name that will be used in the Use Interface for such Task\n    :param image_path: path to the image that represents such Task (used in User Interface)\n    :param datasets_keys: Keys of Datasets that Task takes Scans from\n    :param tags: Label Tags that will be created and assigned to Task\n    :return: Task object\n    \"\"\"\n    with db_session() as session:\n        task = Task(key, name, image_path)\n        datasets = Dataset.query.filter(Dataset.key.in_(datasets_keys)).all()  # type: ignore\n        task.datasets = datasets\n        task.available_tags = tags\n        session.add(task)\n    return task\n\n\ndef assign_label_tag(tag: LabelTag, task_key: str) -> None:\n    \"\"\"Assign existing Label Tag to Task.\n\n    :param tag: tag that should be assigned to Task\n    :param task_key: key that will identify such Task\n    \"\"\"\n    with db_session():\n        task = Task.query.filter(Task.key == task_key).one()\n        task.available_tags.append(tag)\n        task.save()\n\n\ndef unassign_label_tag(tag: LabelTag, task_key: str) -> None:\n    \"\"\"Unassign Label Tag from Task.\n\n    :param tag: tag that should be unassigned from Task\n    :param task_key: key that will identify such Task\n    \"\"\"\n    with db_session():\n        task = Task.query.filter(Task.key == task_key).one()\n        task.available_tags.remove(tag)\n        task.save()\n\n\ndef update(task_key: str, name: str = None, image_path: str = None, datasets_keys: List[str] = None) -> Task:\n    \"\"\"Update Datasets where this Task will be available.\n\n    :param task_key: key that will identify such Task\n    :param name: (optional) new name for such Task\n    :param image_path: (optional) new path to the image which shows on the UI\n    :param datasets_keys: (optional) keys of Datasets which should have this Task\n    \"\"\"\n    with db_session():\n        task = Task.query.filter(Task.key == task_key).one()\n        if name:\n            task.name = name\n        if image_path:\n            task.image_path = image_path\n        if datasets_keys:\n            datasets = Dataset.query.filter(Dataset.key.in_(datasets_keys)).all()  # type: ignore\n            task.datasets = datasets\n    return task\n\n\ndef disable(task_key: str) -> None:\n    \"\"\"Disable existing Task.\"\"\"\n    disabling_query = Task.query.filter(Task.key == task_key)\n    updated = disabling_query.update({'disabled': True}, synchronize_session='fetch')\n    if not updated:\n        raise InternalErrorException(f'Task \"{task_key}\" was not disabled due to unknown database error.')\n\n\ndef enable(task_key: str) -> None:\n    \"\"\"Enable existing Task.\"\"\"\n    enabling_query = Task.query.filter(Task.key == task_key)\n    updated = enabling_query.update({'disabled': False}, synchronize_session='fetch')\n    if not updated:\n        raise InternalErrorException(f'Task \"{task_key}\" was not enabled due to unknown database error.')\n", "path": "backend/medtagger/repositories/tasks.py"}], "after_files": [{"content": "\"\"\"Module responsible for definition of TaskRepository.\"\"\"\nfrom typing import List\n\nfrom medtagger.database import db_session\nfrom medtagger.database.models import Task, LabelTag, Dataset\nfrom medtagger.exceptions import InternalErrorException\n\n\ndef get_all_tasks(include_disabled: bool = False) -> List[Task]:\n    \"\"\"Fetch all tasks from database ordered by key.\"\"\"\n    query = Task.query\n    if not include_disabled:\n        query = query.filter(~Task.disabled)\n    return query.order_by(Task.key).all()\n\n\ndef get_task_by_key(key: str) -> Task:\n    \"\"\"Fetch Task from database.\n\n    :param key: key for a Task\n    :return: Task object\n    \"\"\"\n    with db_session() as session:\n        task = session.query(Task).filter(Task.key == key).first()\n    return task\n\n\ndef add_task(key: str, name: str, image_path: str, datasets_keys: List[str], tags: List[LabelTag]) -> Task:\n    \"\"\"Add new Task to the database.\n\n    :param key: key that will identify such Task\n    :param name: name that will be used in the Use Interface for such Task\n    :param image_path: path to the image that represents such Task (used in User Interface)\n    :param datasets_keys: Keys of Datasets that Task takes Scans from\n    :param tags: Label Tags that will be created and assigned to Task\n    :return: Task object\n    \"\"\"\n    with db_session() as session:\n        task = Task(key, name, image_path)\n        datasets = Dataset.query.filter(Dataset.key.in_(datasets_keys)).all()  # type: ignore\n        task.datasets = datasets\n        task.available_tags = tags\n        session.add(task)\n    return task\n\n\ndef assign_label_tag(tag: LabelTag, task_key: str) -> None:\n    \"\"\"Assign existing Label Tag to Task.\n\n    :param tag: tag that should be assigned to Task\n    :param task_key: key that will identify such Task\n    \"\"\"\n    with db_session():\n        task = Task.query.filter(Task.key == task_key).one()\n        task.available_tags.append(tag)\n        task.save()\n\n\ndef unassign_label_tag(tag: LabelTag, task_key: str) -> None:\n    \"\"\"Unassign Label Tag from Task.\n\n    :param tag: tag that should be unassigned from Task\n    :param task_key: key that will identify such Task\n    \"\"\"\n    with db_session():\n        task = Task.query.filter(Task.key == task_key).one()\n        task.available_tags.remove(tag)\n        task.save()\n\n\ndef update(task_key: str, name: str = None, image_path: str = None, datasets_keys: List[str] = None) -> Task:\n    \"\"\"Update Datasets where this Task will be available.\n\n    :param task_key: key that will identify such Task\n    :param name: (optional) new name for such Task\n    :param image_path: (optional) new path to the image which shows on the UI\n    :param datasets_keys: (optional) keys of Datasets which should have this Task\n    \"\"\"\n    with db_session():\n        task = Task.query.filter(Task.key == task_key).one()\n        if name:\n            task.name = name\n        if image_path:\n            task.image_path = image_path\n        if datasets_keys:\n            datasets = Dataset.query.filter(Dataset.key.in_(datasets_keys)).all()  # type: ignore\n            task.datasets = datasets\n    return task\n\n\ndef disable(task_key: str) -> None:\n    \"\"\"Disable existing Task.\"\"\"\n    disabling_query = Task.query.filter(Task.key == task_key)\n    updated = disabling_query.update({'disabled': True}, synchronize_session='fetch')\n    if not updated:\n        raise InternalErrorException(f'Task \"{task_key}\" was not disabled due to unknown database error.')\n\n\ndef enable(task_key: str) -> None:\n    \"\"\"Enable existing Task.\"\"\"\n    enabling_query = Task.query.filter(Task.key == task_key)\n    updated = enabling_query.update({'disabled': False}, synchronize_session='fetch')\n    if not updated:\n        raise InternalErrorException(f'Task \"{task_key}\" was not enabled due to unknown database error.')\n", "path": "backend/medtagger/repositories/tasks.py"}]}