Abhilash Raj pushed to branch master at GNU Mailman / Mailman Core


Commits:
031fe79d by Abhilash Raj at 2019-04-25T05:06:58Z
Add an error handler in falcon.API for ValueError.

Any unhandled ValueError will be caught by this error handler so that 
client's
don't necessarily need to see 500 error because of bad input.

- - - - -
cc07241a by Abhilash Raj at 2019-04-25T05:06:58Z
Merge branch 'fix-uncaught-errors' into 'master'

Add an error handler in falcon.API for ValueError.

See merge request mailman/mailman!459
- - - - -


1 changed file:

- src/mailman/rest/wsgiapp.py


Changes:

=====================================
src/mailman/rest/wsgiapp.py
=====================================
@@ -25,6 +25,7 @@ from falcon import API, HTTPUnauthorized
 from falcon.routing import map_http_methods, set_default_responders
 from mailman.config import config
 from mailman.database.transaction import transactional
+from mailman.rest.helpers import bad_request
 from mailman.rest.root import Root
 from public import public
 from wsgiref.simple_server import (
@@ -105,6 +106,20 @@ class Middleware:
                 challenges=[realm])
 
 
+def handle_ValueError(exc, request, response, params):
+    """Handle ValueErrors in API code to return HTTPBadRequest.
+
+    ValueErrors are raised often by Validator and should not return a 500 error
+    resposne to the client.  This is a stop-gap for 500 errors due to
+    ValueErrors, it is recommended that they be handled at the call-site,
+    instead of here.
+    """
+    # Only handle ValueError, raise anything else right back.
+    if not isinstance(exc, ValueError):
+        raise exc
+    bad_request(response, str(exc))
+
+
 class ObjectRouter:
     def __init__(self, root):
         self._root = root
@@ -221,7 +236,9 @@ class RootedAPI(API):
 @public
 def make_application():
     """Return a callable WSGI application object."""
-    return RootedAPI(Root())
+    app = RootedAPI(Root())
+    app.add_error_handler(ValueError, handle_ValueError)
+    return app
 
 
 @public



View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/d2a13a4a4d5890abc69c05a43640c0042759bd0c...cc07241af178bb6870e6043babebf0432659734a

-- 
View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/d2a13a4a4d5890abc69c05a43640c0042759bd0c...cc07241af178bb6870e6043babebf0432659734a
You're receiving this email because of your account on gitlab.com.


_______________________________________________
Mailman-checkins mailing list
Mailman-checkins@python.org
Unsubscribe: 
https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to