theLaborInVain / kdm-manager-api

The API used by https://kdm-manager.com and related Kingdom Death: Monster utilities.
Other
3 stars 0 forks source link

Blog needs 404 handling for dynamic routes #38

Closed toconnell closed 3 years ago

toconnell commented 3 years ago

User OID: 666 Method: GET URL: http://api.kdm-manager.com/blog/.well-known/security.txt JSON: None

Traceback (most recent call last):
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
 rv = self.dispatch_request()
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
 return self.view_functions[rule.endpoint](**req.view_args)
File "/home/toconnell/kdm-manager-api/app/routes.py", line 72, in blog_content
 **API.config
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/templating.py", line 140, in render_template
 ctx.app,
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/templating.py", line 120, in _render
 rv = template.render(context)
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/jinja2/environment.py", line 1090, in render
 self.environment.handle_exception()
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/jinja2/environment.py", line 832, in handle_exception
 reraise(*rewrite_traceback_stack(source=source))
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/jinja2/_compat.py", line 28, in reraise
 raise value.with_traceback(tb)
File "/home/toconnell/kdm-manager-api/app/templates/blog/_base.html", line 1, in top-level template code
 {% extends '_base.html' %}
File "/home/toconnell/kdm-manager-api/app/templates/_base.html", line 39, in top-level template code
 {% block body %}{% endblock %}
File "/home/toconnell/kdm-manager-api/app/templates/blog/_base.html", line 29, in block "body"
 {% include 'blog/' + request.view + '.html' %}
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/templating.py", line 60, in get_source
 return self._get_source_fast(environment, template)
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/templating.py", line 89, in _get_source_fast
 raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: blog/.well-known.html
toconnell commented 3 years ago

Added an up-front Jinja check:

diff --git a/app/routes.py b/app/routes.py
index c47d809..88c0280 100644
--- a/app/routes.py
+++ b/app/routes.py
@@ -64,6 +64,12 @@ def blog_content(view, asset):
     flask.request with the 'view' and the 'asset' and then just do our normal
     return for the blog. """

+    # do 404's here, rather than letting Jinja2 do them
+    try:
+        flask.render_template(view)
+    except jinja2.exceptions.TemplateNotFound:
+        return utils.http_404
+
toconnell commented 3 years ago

This will be fixed in the March release.