aboutsummaryrefslogtreecommitdiffstats
path: root/python/fatcat_web
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2020-07-28 16:59:32 -0700
committerBryan Newbold <bnewbold@robocracy.org>2020-07-28 17:29:44 -0700
commitab80084f1cd90faf5e069edc4203668c81e75d63 (patch)
treedd1d4ca21aa805efddd66dd1aa259e5f3b0323d6 /python/fatcat_web
parent76cec8852f6e431b1c87f7d25f8bd4404f1c67e1 (diff)
downloadfatcat-ab80084f1cd90faf5e069edc4203668c81e75d63.tar.gz
fatcat-ab80084f1cd90faf5e069edc4203668c81e75d63.zip
generic API error page
This error handler and view page currently works much better than the "flash()" infrastructure built-in to flask, which uses cookies and mostly does not work with our views and layouts. Would like to gradually migrate almost all API errors in the web interface to just raising errors that get rendered on an error page, instead of calling `abort(ae.status)`.
Diffstat (limited to 'python/fatcat_web')
-rw-r--r--python/fatcat_web/routes.py30
-rw-r--r--python/fatcat_web/templates/api_error.html20
2 files changed, 50 insertions, 0 deletions
diff --git a/python/fatcat_web/routes.py b/python/fatcat_web/routes.py
index 77b5dc55..a6634292 100644
--- a/python/fatcat_web/routes.py
+++ b/python/fatcat_web/routes.py
@@ -1005,6 +1005,36 @@ def page_server_error(e):
def page_server_down(e):
return render_template('503.html'), 503
+@app.errorhandler(ApiException)
+def page_fatcat_api_error(ae):
+ """
+ Generic error handler for fatcat API problems. With this error handler,
+ don't need to explicitly catch API exceptions: they should get caught and
+ routed correctly here.
+ """
+ if ae.status == 404:
+ return page_not_found(ae)
+ elif ae.status in [401, 403]:
+ return page_not_authorized(ae)
+ elif ae.status in [405]:
+ return page_method_not_allowed(ae)
+ elif ae.status in [409]:
+ return page_edit_conflict(ae)
+ try:
+ json_body = json.loads(ae.body)
+ ae.error_name = json_body.get('error')
+ ae.message = json_body.get('message')
+ except ValueError:
+ pass
+ return render_template('api_error.html', api_error=ae), ae.status
+
+@app.errorhandler(ApiValueError)
+def page_fatcat_api_value_error(ae):
+ ae.status = 400
+ ae.error_name = "ValueError"
+ ae.message = str(ae)
+ return render_template('api_error.html', api_error=ae), 400
+
@app.errorhandler(CSRFError)
def page_csrf_error(e):
return render_template('csrf_error.html', reason=e.description), 400
diff --git a/python/fatcat_web/templates/api_error.html b/python/fatcat_web/templates/api_error.html
new file mode 100644
index 00000000..1a44f610
--- /dev/null
+++ b/python/fatcat_web/templates/api_error.html
@@ -0,0 +1,20 @@
+{% extends "base.html" %}
+{% block body %}
+
+<center>
+<div style="font-size: 8em;">{{ api_error.status or "" }}</div>
+<div style="font-size: 3em;">{{ api_error.reason or "" }}</div>
+</center>
+
+<div class="ui icon error message">
+ <i class="ban icon"></i>
+ <div class="content">
+ <div class="header">
+ API Error: {{ api_error.error_name or "" }}
+ </div>
+ <p>{{ api_error.message or "" }}
+ </div>
+</div>
+
+
+{% endblock %}