[UPD] Exception handling
This commit is contained in:
@@ -16,6 +16,7 @@ def handle_exception(e):
|
||||
"status_code": e.status_code,
|
||||
"name": e.name,
|
||||
"error": e.error,
|
||||
"data": e.data
|
||||
})
|
||||
response.content_type = "application/json"
|
||||
response.status_code = e.status_code
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
from flask import request
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from .blueprint import api_library
|
||||
|
||||
|
||||
@@ -12,11 +12,7 @@ logger = logging.getLogger(__name__)
|
||||
@api_library.route("/<_id>", methods=["DELETE"])
|
||||
def delete_library(_id):
|
||||
|
||||
try: # TODO: function
|
||||
controller = LibraryController(_id)
|
||||
except NoResultFound as e:
|
||||
logger.debug({e})
|
||||
return { "status": "error", "error": "Library not found" }, 404
|
||||
controller = LibraryController(_id)
|
||||
|
||||
controller.delete()
|
||||
|
||||
|
||||
@@ -17,11 +17,5 @@ def read_libraries():
|
||||
|
||||
@api_library.route("/<_id>", methods=["GET"])
|
||||
def read_library(_id):
|
||||
try:
|
||||
library = LibraryController(_id)
|
||||
except NoResultFound as e:
|
||||
logger.debug(f"No result found for Library wid id {_id}")
|
||||
logger.debug(f"Error {e}")
|
||||
logger.debug(f"Error {dir(e)}")
|
||||
return { "status": "error", "result": "Library not found"}, 404
|
||||
library = LibraryController(_id)
|
||||
return { "status": "ok", "result": library.data.to_dict() }, 200
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user