]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
restful: Use pecan hook instead of catch decorator
authorBoris Ranto <branto@redhat.com>
Thu, 18 May 2017 17:27:55 +0000 (19:27 +0200)
committerBoris Ranto <branto@redhat.com>
Mon, 22 May 2017 17:21:28 +0000 (19:21 +0200)
This removes the catch decorator which was useful primarily for testing
purposes and instead switches to a pecan error hook which will print the
traceback to the ceph-mgr log.

Signed-off-by: Boris Ranto <branto@redhat.com>
12 files changed:
src/pybind/mgr/restful/api/__init__.py
src/pybind/mgr/restful/api/config.py
src/pybind/mgr/restful/api/crush.py
src/pybind/mgr/restful/api/doc.py
src/pybind/mgr/restful/api/mon.py
src/pybind/mgr/restful/api/osd.py
src/pybind/mgr/restful/api/pool.py
src/pybind/mgr/restful/api/request.py
src/pybind/mgr/restful/api/server.py
src/pybind/mgr/restful/decorators.py
src/pybind/mgr/restful/hooks.py [new file with mode: 0644]
src/pybind/mgr/restful/module.py

index e04efca8213e57e353d703dbe3a96fc8d8b25678..a6c662a2ca13f00c39e7c0a7518450b0f7e9ce8b 100644 (file)
@@ -10,8 +10,6 @@ from pool import Pool
 from request import Request
 from server import Server
 
-from restful.decorators import catch
-
 
 class Root(RestController):
     config = Config()
@@ -24,7 +22,6 @@ class Root(RestController):
     server = Server()
 
     @expose(template='json')
-    @catch
     def get(self, **kwargs):
         """
         Show the basic information for the REST API
index cca6a472829ace70eb2e8d605b51785aafc8b817..51fabfad4d68ed0f5cc61a902aa494095208870d 100644 (file)
@@ -2,12 +2,11 @@ from pecan import expose, request
 from pecan.rest import RestController
 
 from restful import common, module
-from restful.decorators import auth, catch
+from restful.decorators import auth
 
 
 class ConfigOsd(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -22,7 +21,6 @@ class ConfigOsd(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def patch(self, **kwargs):
         """
@@ -58,7 +56,6 @@ class ConfigClusterKey(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -70,7 +67,6 @@ class ConfigClusterKey(RestController):
 
 class ConfigCluster(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
index 14338d6b566c360382c343cbe0899b6e6ee2ce46..046676a2815fb6a16d2017323bb571e703ce6d5d 100644 (file)
@@ -4,12 +4,11 @@ from pecan.rest import RestController
 from restful import common, module
 from collections import defaultdict
 
-from restful.decorators import auth, catch
+from restful.decorators import auth
 
 
 class CrushRuleset(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -29,7 +28,6 @@ class CrushRuleset(RestController):
 
 class CrushRule(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
index 3ccd99b9df8c477dd9dea29998d5b179b529df45..96e4d6a3379b5a7062746d3b2f0c4e081f94a72f 100644 (file)
@@ -2,14 +2,12 @@ from pecan import expose
 from pecan.rest import RestController
 
 from restful import module
-from restful.decorators import catch
 
 import restful
 
 
 class Doc(RestController):
     @expose(template='json')
-    @catch
     def get(self, **kwargs):
         """
         Show documentation information
index c47e75335cb2ceeffc3c58a91da25956bb853ad8..5b19aaefb299dd6a2bc02bb1a6d8740e126ba3ef 100644 (file)
@@ -2,7 +2,7 @@ from pecan import expose, response
 from pecan.rest import RestController
 
 from restful import module
-from restful.decorators import auth, catch
+from restful.decorators import auth
 
 
 class MonName(RestController):
@@ -11,7 +11,6 @@ class MonName(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -32,7 +31,6 @@ class MonName(RestController):
 
 class Mon(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
index d962031d9ef1744494fdc803988901291136a0fb..b42f33941f1c312b93f9e389834f257b163b1f1d 100644 (file)
@@ -2,7 +2,7 @@ from pecan import expose, request, response
 from pecan.rest import RestController
 
 from restful import common, module
-from restful.decorators import auth, catch
+from restful.decorators import auth
 
 
 class OsdIdCommand(RestController):
@@ -11,7 +11,6 @@ class OsdIdCommand(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -30,7 +29,6 @@ class OsdIdCommand(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def post(self, **kwargs):
         """
@@ -62,7 +60,6 @@ class OsdId(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -77,7 +74,6 @@ class OsdId(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def patch(self, **kwargs):
         """
@@ -122,7 +118,6 @@ class OsdId(RestController):
 
 class Osd(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
index dca450ee501e2e555a383d1fe52054c2d88a8910..0c8d2c92e63afc23e45c1dd72888c29ef3194315 100644 (file)
@@ -2,7 +2,7 @@ from pecan import expose, request, response
 from pecan.rest import RestController
 
 from restful import common, module
-from restful.decorators import auth, catch
+from restful.decorators import auth
 
 
 class PoolId(RestController):
@@ -11,7 +11,6 @@ class PoolId(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -30,7 +29,6 @@ class PoolId(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def patch(self, **kwargs):
         """
@@ -55,7 +53,6 @@ class PoolId(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def delete(self, **kwargs):
         """
@@ -78,7 +75,6 @@ class PoolId(RestController):
 
 class Pool(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -95,7 +91,6 @@ class Pool(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def post(self, **kwargs):
         """
index ddf422bba2cae29825cc5454c738ed4357e9500c..30d248a97e7e98c6899eb98b18ae33d726cddc4f 100644 (file)
@@ -2,7 +2,7 @@ from pecan import expose, request, response
 from pecan.rest import RestController
 
 from restful import module
-from restful.decorators import auth, catch, lock
+from restful.decorators import auth, lock
 
 
 class RequestId(RestController):
@@ -11,7 +11,6 @@ class RequestId(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -31,7 +30,6 @@ class RequestId(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     @lock
     def delete(self, **kwargs):
@@ -50,7 +48,6 @@ class RequestId(RestController):
 
 class Request(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -64,7 +61,6 @@ class Request(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     @lock
     def delete(self, **kwargs):
@@ -86,7 +82,6 @@ class Request(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def post(self, **kwargs):
         """
index 99bf22bc52ccc97fa034f7b0d815c7b2238409c6..9e6a20784ec0b7d0dbaa23086e170de1f719a5ab 100644 (file)
@@ -2,7 +2,7 @@ from pecan import expose
 from pecan.rest import RestController
 
 from restful import module
-from restful.decorators import auth, catch
+from restful.decorators import auth
 
 
 class ServerFqdn(RestController):
@@ -11,7 +11,6 @@ class ServerFqdn(RestController):
 
 
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
@@ -23,7 +22,6 @@ class ServerFqdn(RestController):
 
 class Server(RestController):
     @expose(template='json')
-    @catch
     @auth
     def get(self, **kwargs):
         """
index fd12b0202017a544987af4663a848744d04400c2..09fa64dca5360bac25040c67053f97af75918338 100644 (file)
@@ -34,19 +34,6 @@ def auth(f):
     return decorated
 
 
-# Helper function to catch and log the exceptions
-def catch(f):
-    @wraps(f)
-    def decorated(*args, **kwargs):
-        try:
-            return f(*args, **kwargs)
-        except:
-            module.instance.log.error(str(traceback.format_exc()))
-            response.status = 500
-            return {'message': str(traceback.format_exc()).split('\n')}
-    return decorated
-
-
 # Helper function to lock the function
 def lock(f):
     @wraps(f)
diff --git a/src/pybind/mgr/restful/hooks.py b/src/pybind/mgr/restful/hooks.py
new file mode 100644 (file)
index 0000000..3fd244a
--- /dev/null
@@ -0,0 +1,9 @@
+from pecan.hooks import PecanHook
+
+import traceback
+
+import module
+
+class ErrorHook(PecanHook):
+    def on_error(self, stat, exc):
+        module.instance.log.error(str(traceback.format_exc()))
index 71c9d26b3d8c5274db925728b72e2991f55206d1..6e643e862b56e656c6ad7d42d85ba1de114a8f5f 100644 (file)
@@ -17,6 +17,7 @@ from pecan import jsonify, make_app
 from pecan.rest import RestController
 from werkzeug.serving import make_server, make_ssl_devcert
 
+from hooks import ErrorHook
 from mgr_module import MgrModule, CommandResult
 
 # Global instance to share
@@ -234,7 +235,10 @@ class Module(MgrModule):
         self.server = make_server(
             host='0.0.0.0',
             port=8003,
-            app=make_app('restful.api.Root'),
+            app=make_app(
+                root='restful.api.Root',
+                hooks = lambda: [ErrorHook()],
+            ),
             ssl_context=(cert, pkey),
         )