You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by hu...@apache.org on 2020/09/10 10:06:25 UTC

[incubator-ponymail-foal] 01/02: Allow for traceback to be printed to the journal (stderr) instead of to the client

This is an automated email from the ASF dual-hosted git repository.

humbedooh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git

commit 98633b28fb3495543067e536b28bdab088f6f470
Author: Daniel Gruno <hu...@apache.org>
AuthorDate: Thu Sep 10 12:03:57 2020 +0200

    Allow for traceback to be printed to the journal (stderr) instead of to the client
---
 INSTALL.md                      |  8 +++++++-
 server/main.py                  | 17 ++++++++++++++---
 server/plugins/configuration.py |  4 ++++
 tools/setup.py                  |  1 +
 4 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/INSTALL.md b/INSTALL.md
index 46478b2..51cbe7c 100644
--- a/INSTALL.md
+++ b/INSTALL.md
@@ -127,4 +127,10 @@ separating each entry with a single space:
 - to allow email for all lists at any subdomain under mydomain.tld, you should add `*.mydomain.tld`
 
 Only users logged in via authoritative OAuth will be able to compose replies via the
-web interface.
\ No newline at end of file
+web interface.
+
+## Hiding tracebacks from users
+By default, API errors will include a full traceback for debugging purposes. If you wish to 
+instead have this be printed to the system journal (`stderr`), you can set the `traceback`
+option to `false` in `server/ponymail.yaml`. This will instead print an error ID to the user, 
+corresponding to a traceback in stderr.
diff --git a/server/main.py b/server/main.py
index 45c64ec..22161ed 100644
--- a/server/main.py
+++ b/server/main.py
@@ -27,6 +27,7 @@ import traceback
 
 import aiohttp.web
 import yaml
+import uuid
 
 import plugins.background
 import plugins.configuration
@@ -132,9 +133,19 @@ class Server(plugins.server.BaseServer):
                 err = "\n".join(
                     traceback.format_exception(exc_type, exc_value, exc_traceback)
                 )
-                return aiohttp.web.Response(
-                    headers=headers, status=500, text="API error occurred: " + err
-                )
+                if self.config.ui.traceback:
+                    return aiohttp.web.Response(
+                        headers=headers, status=500, text="API error occurred: " + err
+                    )
+                else:
+                    eid = str(uuid.uuid4())[:18]
+                    sys.stderr.write("API Endpoint %s got into trouble (%s): \n" % (request.path, eid))
+                    for line in err.split("\n"):
+                        sys.stderr.write("%s: %s\n" % (eid, line))
+                    return aiohttp.web.Response(
+                        headers=headers, status=500, text="API error occurred. The application journal will have "
+                                                          "information. Error ID: %s" % eid
+                    )
         else:
             return aiohttp.web.Response(
                 headers=headers, status=404, text="API Endpoint not found!"
diff --git a/server/plugins/configuration.py b/server/plugins/configuration.py
index 7ab746b..4546a59 100644
--- a/server/plugins/configuration.py
+++ b/server/plugins/configuration.py
@@ -18,11 +18,15 @@ class UIConfig:
     wordcloud:      bool
     mailhost:       str
     sender_domains: str
+    traceback:      bool
 
     def __init__(self, subyaml: dict):
         self.wordcloud = bool(subyaml.get('wordcloud', False))
         self.mailhost = subyaml.get('mailhost', '')  # Default to nothing (disabled)
         self.sender_domains = subyaml.get('sender_domains', '')  # Default to nothing (disabled)
+        # Default to spitting out traceback to web clients
+        # Set to false in yaml to redirect to stderr instead.
+        self.traceback = subyaml.get('traceback', True)
 
 
 class OAuthConfig:
diff --git a/tools/setup.py b/tools/setup.py
index bcc8ccc..cbbadc9 100755
--- a/tools/setup.py
+++ b/tools/setup.py
@@ -451,6 +451,7 @@ ui:
   wordcloud:       %s
   mailhost:        %s
   sender_domains:  %s
+  traceback:       true
 
 tasks:
   refresh_rate:  150     # Background indexer run interval, in seconds