1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 13:04:19 +00:00

Added error handler

Now errors are returned in json format
This commit is contained in:
Lorenzo Balugani 2021-04-29 00:07:43 +02:00
parent 7ff3fd5e1c
commit 39eab92808
3 changed files with 10 additions and 3 deletions

View file

@ -16,7 +16,6 @@ jwt = JWTManager(app)
cors = CORS(app) cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type' 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) 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 # 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/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.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__": if __name__ == "__main__":
with app.app_context(): with app.app_context():
Base.create_all(app=app) Base.create_all(app=app)
if not User.query.filter_by(isAdmin=True).all(): 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() Base.session.commit()
debug = True debug = True
if os.getenv("DISABLE_DEBUG"): if os.getenv("DISABLE_DEBUG"):

View file

@ -96,3 +96,8 @@ def json_success(data):
:return: a json formatted string :return: a json formatted string
""" """
return jsonify({"result": "success", "data": data}) return jsonify({"result": "success", "data": data})
def error_handler(e):
print(f"{e.description} - {e.code}")
return json_error(f"{e.description} - {e.code}")

View file

@ -18,7 +18,7 @@ def page_user_create():
""" """
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if not user.isAdmin: 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")), new_user = User(email=request.json.get("email"), password=gen_password(request.json.get("password")),
username=request.json.get("username")) username=request.json.get("username"))
Base.session.add(new_user) Base.session.add(new_user)