about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBonfaceKilz2021-09-27 13:07:11 +0300
committerBonfaceKilz2021-09-27 13:22:01 +0300
commit3494a9af7f82691f419ba1e3bd616be5ce68a00a (patch)
tree03b5592be8c8a4cc0db0bd27718b136e5afd2ae9
parentcb9422ec5fc985530fcb4fb44d2729e460424d60 (diff)
downloadgenenetwork2-3494a9af7f82691f419ba1e3bd616be5ce68a00a.tar.gz
Remove lengthy stack trace for 404, 400, 408 status codes
The stack trace causes the log file to grow into unmanageable sizes
over time.
-rw-r--r--wqflask/wqflask/views.py26
1 files changed, 17 insertions, 9 deletions
diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py
index 980b9362..1139ad43 100644
--- a/wqflask/wqflask/views.py
+++ b/wqflask/wqflask/views.py
@@ -160,18 +160,26 @@ def shutdown_session(exception=None):
 
 
 @app.errorhandler(Exception)
-def handle_bad_request(e):
+def handle_generic_exceptions(e):
+    import werkzeug
     err_msg = str(e)
-    logger.error(err_msg)
-    logger.error(request.url)
-    # get the stack trace and send it to the logger
-    exc_type, exc_value, exc_traceback = sys.exc_info()
-    logger.error(traceback.format_exc())
     now = datetime.datetime.utcnow()
     time_str = now.strftime('%l:%M%p UTC %b %d, %Y')
-    formatted_lines = [request.url
-                       + " (" + time_str + ")"] + traceback.format_exc().splitlines()
-
+    # get the stack trace and send it to the logger
+    exc_type, exc_value, exc_traceback = sys.exc_info()
+    formatted_lines = {f"{request.url} ({time_str}) "
+                       f" {traceback.format_exc().splitlines()}"}
+
+    _message_templates = {
+        werkzeug.exceptions.NotFound: ("404: Not Found: "
+                                       f"{time_str}: {request.url}"),
+        werkzeug.exceptions.BadRequest: ("400: Bad Request: "
+                                         f"{time_str}: {request.url}"),
+        werkzeug.exceptions.RequestTimeout: ("408: Request Timeout: "
+                                             f"{time_str}: {request.url}")}
+    # Default to the lengthy stack trace!
+    logger.error(_message_templates.get(exc_type,
+                                        formatted_lines))
     # Handle random animations
     # Use a cookie to have one animation on refresh
     animation = request.cookies.get(err_msg[:32])