[WIP] Exception handling

This commit is contained in:
2026-02-15 17:39:21 +01:00
parent 7c3593d066
commit 2a8a44e80b
7 changed files with 103 additions and 15 deletions

View File

@@ -1 +1,39 @@
from .api import api
from flask import json, make_response
from werkzeug.exceptions import HTTPException
from .api import api
from ..controller.exceptions import LibraryExceptionBase
@api.errorhandler(LibraryExceptionBase)
def handle_exception(e):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = make_response()
# replace the body with JSON
response.data = json.dumps({
"status": "error",
"code": e.code,
"status_code": e.status_code,
"name": e.name,
"error": e.error,
})
response.content_type = "application/json"
response.status_code = e.status_code
return response
@api.errorhandler(HTTPException)
def handle_exception(e):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = e.get_response()
# replace the body with JSON
response.data = json.dumps({
"status": "error",
"code": f"000{e.code}",
"status_code": e.code,
"name": e.name,
"error": e.description,
})
response.content_type = "application/json"
return response

View File

@@ -11,18 +11,10 @@ logger = logging.getLogger(__name__)
@api_library.route("/", methods=["POST"])
def create_library():
try:
data = request.json
except Exception as e:
logger.debug(f"{e}")
return { "status": "error", "error": "JSON Required" }, 415
data = request.json
with LibraryController() as controller:
try:
lib = Library(**data)
library = controller.create(lib)
except IntegrityError as e:
logger.debug(f"DB Error Creating {e}")
return { "status": "error", "error": f"{e.orig}" }, 400
else:
return { "status": "ok", "result": library.data.to_dict() }, 200
lib = Library(**data)
library = controller.create(lib)
return { "status": "ok", "result": library.data.to_dict() }, 200