Merge pull request #196 from SibrenVasse/error_handling
Move api error handling to global error handler
This commit is contained in:
commit
1997c207ed
|
@ -33,19 +33,3 @@ def require_api_auth(f):
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
|
||||||
return decorated
|
return decorated
|
||||||
|
|
||||||
|
|
||||||
@api_bp.app_errorhandler(404)
|
|
||||||
def not_found(e):
|
|
||||||
return jsonify(error="No such endpoint"), 404
|
|
||||||
|
|
||||||
|
|
||||||
@api_bp.app_errorhandler(Exception)
|
|
||||||
def internal_error(e):
|
|
||||||
LOG.exception(e)
|
|
||||||
return jsonify(error="Internal error"), 500
|
|
||||||
|
|
||||||
|
|
||||||
@api_bp.app_errorhandler(405)
|
|
||||||
def wrong_method(e):
|
|
||||||
return jsonify(error="Method not allowed"), 405
|
|
||||||
|
|
28
server.py
28
server.py
|
@ -334,25 +334,47 @@ def setup_openid_metadata(app):
|
||||||
|
|
||||||
def setup_error_page(app):
|
def setup_error_page(app):
|
||||||
@app.errorhandler(400)
|
@app.errorhandler(400)
|
||||||
def page_not_found(e):
|
def bad_request(e):
|
||||||
|
if request.path.startswith("/api/"):
|
||||||
|
return jsonify(error="Bad Request"), 400
|
||||||
|
else:
|
||||||
return render_template("error/400.html"), 400
|
return render_template("error/400.html"), 400
|
||||||
|
|
||||||
@app.errorhandler(401)
|
@app.errorhandler(401)
|
||||||
def page_not_found(e):
|
def unauthorized(e):
|
||||||
|
if request.path.startswith("/api/"):
|
||||||
|
return jsonify(error="Unauthorized"), 401
|
||||||
|
else:
|
||||||
flash("You need to login to see this page", "error")
|
flash("You need to login to see this page", "error")
|
||||||
return redirect(url_for("auth.login", next=request.full_path))
|
return redirect(url_for("auth.login", next=request.full_path))
|
||||||
|
|
||||||
@app.errorhandler(403)
|
@app.errorhandler(403)
|
||||||
def page_not_found(e):
|
def forbidden(e):
|
||||||
|
if request.path.startswith("/api/"):
|
||||||
|
return jsonify(error="Forbidden"), 403
|
||||||
|
else:
|
||||||
return render_template("error/403.html"), 403
|
return render_template("error/403.html"), 403
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
def page_not_found(e):
|
def page_not_found(e):
|
||||||
|
if request.path.startswith("/api/"):
|
||||||
|
return jsonify(error="No such endpoint"), 404
|
||||||
|
else:
|
||||||
return render_template("error/404.html"), 404
|
return render_template("error/404.html"), 404
|
||||||
|
|
||||||
|
@app.errorhandler(405)
|
||||||
|
def wrong_method(e):
|
||||||
|
if request.path.startswith("/api/"):
|
||||||
|
return jsonify(error="Method not allowed"), 405
|
||||||
|
else:
|
||||||
|
return render_template("error/405.html"), 405
|
||||||
|
|
||||||
@app.errorhandler(Exception)
|
@app.errorhandler(Exception)
|
||||||
def error_handler(e):
|
def error_handler(e):
|
||||||
LOG.exception(e)
|
LOG.exception(e)
|
||||||
|
if request.path.startswith("/api/"):
|
||||||
|
return jsonify(error="Internal error"), 500
|
||||||
|
else:
|
||||||
return render_template("error/500.html"), 500
|
return render_template("error/500.html"), 500
|
||||||
|
|
||||||
|
|
||||||
|
|
15
templates/error/405.html
Normal file
15
templates/error/405.html
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{% extends "error.html" %}
|
||||||
|
|
||||||
|
{% block error_name %}
|
||||||
|
405
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block error_description %}
|
||||||
|
Client used wrong method when accessing resource.
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block suggestion %}
|
||||||
|
<a class="btn btn-primary" href="javascript:history.back()">
|
||||||
|
<i class="fe fe-arrow-left mr-2"></i>Go Back
|
||||||
|
</a>
|
||||||
|
{% endblock %}
|
Loading…
Reference in a new issue