[UPD] Exception handling

This commit is contained in:
2026-02-15 20:53:32 +01:00
parent 2a8a44e80b
commit 969333998c
17 changed files with 105 additions and 81 deletions

View File

@@ -4,47 +4,24 @@ from sqlalchemy.exc import IntegrityError, NoResultFound
from .blueprint import api_library
from ....controller import LibraryController
from ....controller.functions import update_item_key
from ....schema.library.library import Library
import logging
logger = logging.getLogger(__name__)
def update_library_item(library:Library, key, value):
if key == "id":
raise AttributeError("id is not updatable")
try:
library.__getattribute__(key)
except AttributeError:
raise AttributeError(f"{key} not in library")
library.__setattr__(key, value)
@api_library.route("/<_id>", methods=["PATCH"])
def update_library(_id):
try:
data:dict = request.json
logger.debug(f"data: {data}")
except Exception as e:
logger.debug(f"{e}")
return { "status": "error", "error": "JSON Required" }, 415
try: # TODO: function
controller = LibraryController(_id)
except NoResultFound as e:
logger.debug({e})
return { "status": "error", "error": "Library not found" }, 404
data:dict = request.json
controller = LibraryController(_id)
library = controller.data
for key, value in data.items():
try:
update_library_item(library, key, value)
except AttributeError as e:
logger.debug(f"Error updating {e}")
return { "status": "error", "error": e.name }
try:
controller.update()
except IntegrityError as e:
logger.debug(f"DB Error Creating {e}")
return { "status": "error", "error": f"{e.orig}" }, 400
else:
return { "status": "ok", "result": controller.data.to_dict() }, 200
update_item_key(library, key, value)
controller.update()
return { "status": "ok", "result": controller.data.to_dict() }, 200