From 66d6e2480fb9c0e71e2436171ccc15eace4429d5 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Mon, 6 Sep 2021 01:39:50 +0200 Subject: [PATCH] Clean up error handlers --- backend/error_handlers.py | 13 +++++++++---- backend/server.py | 8 +++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/backend/error_handlers.py b/backend/error_handlers.py index 616710e..3a9bdb5 100644 --- a/backend/error_handlers.py +++ b/backend/error_handlers.py @@ -1,6 +1,11 @@ -def handler_404(e=None): - return {"error": e or "The requested resource could not be found."}, 404 +from werkzeug.exceptions import NotFound, UnprocessableEntity -def handler_422(e): - return {"error": e}, 422 +def handler_404(e: NotFound): + return {"error": e.description}, 404 + + +def handler_422(e: UnprocessableEntity): + print(e) + print(e.description) + return {"error": e.description}, 422 diff --git a/backend/server.py b/backend/server.py index 54ab277..1f190fe 100644 --- a/backend/server.py +++ b/backend/server.py @@ -1,9 +1,11 @@ +from werkzeug.exceptions import abort + from .error_handlers import handler_404, handler_422 from .routes.dm import dm_blueprint from .routes.ping import ping_blueprint from .routes.stats import stats_blueprint from functions.database import custom_commands -from quart import Quart, jsonify +from quart import Quart, jsonify, Response from quart_cors import cors @@ -36,12 +38,12 @@ async def get_custom_command(command_id): command_id = int(command_id) except ValueError: # Id is not an int - return unprocessable_entity("Parameter id was not a valid integer.") + abort(422, "Parameter id was not a valid integer.") command = custom_commands.get_by_id(command_id) if command is None: - return handler_404("") + abort(404) return jsonify(command)