From 39eab92808e2e995fd55633e84f6e71eea547656 Mon Sep 17 00:00:00 2001 From: Lorenzo Balugani Date: Thu, 29 Apr 2021 00:07:43 +0200 Subject: [PATCH] Added error handler Now errors are returned in json format --- code/backend/nest_backend/__main__.py | 6 ++++-- code/backend/nest_backend/gestione.py | 5 +++++ code/backend/nest_backend/routes/users/user_create.py | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/code/backend/nest_backend/__main__.py b/code/backend/nest_backend/__main__.py index 86633e9..e8e181b 100644 --- a/code/backend/nest_backend/__main__.py +++ b/code/backend/nest_backend/__main__.py @@ -16,7 +16,6 @@ jwt = JWTManager(app) cors = CORS(app) app.config['CORS_HEADERS'] = 'Content-Type' - reverse_proxy_app = werkzeug.middleware.proxy_fix.ProxyFix(app=app, x_for=1, x_proto=0, x_host=1, x_port=0, x_prefix=0) # Routes setup @@ -29,11 +28,14 @@ app.add_url_rule("/api/repository/create", view_func=page_repository_create, met app.add_url_rule("/api/repository/edit", view_func=page_repository_edit, methods=["PUT"]) app.add_url_rule("/api/repository/add_condition", view_func=page_repository_add_condition, methods=["POST"]) +app.register_error_handler(Exception, error_handler) + if __name__ == "__main__": with app.app_context(): Base.create_all(app=app) if not User.query.filter_by(isAdmin=True).all(): - Base.session.add(User(email="admin@admin.com", password=gen_password("password"), username="admin", isAdmin=True)) + Base.session.add( + User(email="admin@admin.com", password=gen_password("password"), username="admin", isAdmin=True)) Base.session.commit() debug = True if os.getenv("DISABLE_DEBUG"): diff --git a/code/backend/nest_backend/gestione.py b/code/backend/nest_backend/gestione.py index 22c8a66..f998d42 100644 --- a/code/backend/nest_backend/gestione.py +++ b/code/backend/nest_backend/gestione.py @@ -96,3 +96,8 @@ def json_success(data): :return: a json formatted string """ return jsonify({"result": "success", "data": data}) + + +def error_handler(e): + print(f"{e.description} - {e.code}") + return json_error(f"{e.description} - {e.code}") diff --git a/code/backend/nest_backend/routes/users/user_create.py b/code/backend/nest_backend/routes/users/user_create.py index 43fb5a1..1f36faf 100644 --- a/code/backend/nest_backend/routes/users/user_create.py +++ b/code/backend/nest_backend/routes/users/user_create.py @@ -18,7 +18,7 @@ def page_user_create(): """ user = find_user(get_jwt_identity()) if not user.isAdmin: - abort(403) + json_error("User is not admin. Thou art not authorized."), 403 new_user = User(email=request.json.get("email"), password=gen_password(request.json.get("password")), username=request.json.get("username")) Base.session.add(new_user)