Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Handle errors and http exceptions
core/app.py | 23 ++++++++++++++++++++++-
diff --git a/core/app.py b/core/app.py index 4dea414b8de7d63ea323884dc788e4a5d259e830..c5c4cbcc1573e744ca9c883a95413eb50b3fdbc5 100644 --- a/core/app.py +++ b/core/app.py @@ -1,4 +1,4 @@ -from flask import Flask, abort +from flask import Flask, abort, render_template, request from jinja2 import Markup import locale from core.config import read_from_config, env @@ -50,6 +50,27 @@ @self.template_filter() def md(text): return mistune.markdown(text) + + @self.errorhandler(422) + def handle_unprocessable_entity(e): + if request.path.startswith("/api"): + return { "errors": [ { "reason": "422 Unprocessable Entity" } ] }, 422 + + return render_template("unprocessable_entity.html", 422) + + @self.errorhandler(500) + def handle_internal_server_error(e): + if request.path.startswith("/api"): + return { "errors": [ { "reason": "500 Internal Server Error" } ] }, 500 + + return render_template("internal_error.html", 500) + + @self.errorhandler(404) + def handle_not_found(e): + if request.path.startswith("/api"): + return { "errors": [ { "reason": "404 Not Found" } ] }, 404 + + return render_template("not_found.html"), 404 @self.context_processor def inject():