Repository: agdsn/pycroft
Issue: Unhandled exception on invalid room formdata in login

See this piece of code: https://github.com/agdsn/pycroft/blob/37673af7588a608b70bbacdf2fccee63a358dc58/web/blueprints/user/__init__.py#L736-L739

That should instead be a `one_or_none()` with an error on failure

Changed files:
web/blueprints/host/__init__.py, web/blueprints/helpers/exception.py

diff --git a/web/blueprints/helpers/exception.py b/web/blueprints/helpers/exception.py
index 0801bea6..7b1473f2 100644
--- a/web/blueprints/helpers/exception.py
+++ b/web/blueprints/helpers/exception.py
@@ -55,6 +55,7 @@ def handle_errors(session: Session):
         raise
     except Exception as e:
         traceback.print_exc()
+        logger.exception("Unexpected error when handling web response", stack_info=True)
         flash(f"Es ist ein unerwarteter Fehler aufgetreten: {e}", "error")
         session.rollback()
         raise UnexpectedException from e
diff --git a/web/blueprints/host/__init__.py b/web/blueprints/host/__init__.py
index 4c9aacc1..7e9d6e18 100644
--- a/web/blueprints/host/__init__.py
+++ b/web/blueprints/host/__init__.py
@@ -10,6 +10,7 @@ from pycroft.exc import PycroftException
 from pycroft.helpers.net import mac_regex, get_interface_manufacturer
 from pycroft.lib import host as lib_host
 from pycroft.lib.net import get_subnets_for_room, get_unused_ips
+from pycroft.lib.facilities import get_room
 from pycroft.model import session
 from pycroft.model.facilities import Room
 from pycroft.model.host import Host, Interface
@@ -86,13 +87,19 @@ def host_edit(host_id):
     if not form.validate():
         return default_response()
 
-    room = Room.q.filter_by(number=form.room_number.data,
-                            level=form.level.data,
-                            building=form.building.data).one()
+    # existence guaranteed by validator
     owner = User.get(form.owner_id.data)
-
     try:
         with handle_errors(session.session):
+            if not (
+                room := get_room(
+                    building_id=form.building.data,
+                    level=form.level.data,
+                    room_number=form.room_number.data,
+                )
+            ):
+                form.room_number.errors.append("room does not exist")
+                return default_response()
             lib_host.host_edit(
                 host, owner, room, form.name.data,
                 processor=current_user
@@ -130,12 +137,20 @@ def host_create():
     if not form.validate_on_submit():
         return default_response()
 
-    room = Room.q.filter_by(number=form.room_number.data,
-                            level=form.level.data,
-                            building=form.building.data).one()
+    # existence verified by validator
     owner = User.get(form.owner_id.data)
     try:
         with handle_errors(session.session):
+            if not (
+                room := get_room(
+                    building_id=form.building.data,
+                    level=form.level.data,
+                    room_number=form.room_number.data,
+                )
+            ):
+                form.room_number.errors.append("room does not exist")
+                return default_response()
+
             host = lib_host.host_create(
                 owner, room, form.name.data, processor=current_user
             )