Commits

jayven committed 0fbc5bd

Add utils.py

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# vim: set ai et si ts=4 sts=4 sw=4:
+
+import sys
+
+
+def _hook_excpetion():
+
+    # id->callback to call when exception is caught
+    _exception_cb = {}
+    
+    def register_exception_cb(id, cb):
+        _exception_cb[id] = cb
+
+    def unregister_exception_cb(id):
+        _exception_cb.pop(id, None)
+
+    def _wrap_dispatch(old):
+        def dispatch(self, method, auth, params):
+            try:
+                return old(self, method, auth, params)
+            except:
+                t, v, tb = sys.exc_info() 
+                for cb in _exception_cb.itervalues():
+                    cb(t, v, tb)
+                raise
+        return dispatch
+
+    from netsvc import ExportService
+    for service in ExportService._services.itervalues():
+        dispatch = service.__class__.__dict__.get('dispatch', None)
+        if dispatch:
+            service.__class__.dispatch = _wrap_dispatch(dispatch)
+
+    return register_exception_cb, unregister_exception_cb
+
+
+register_exception_cb, unregister_exception_cb = _hook_excpetion()
+del _hook_excpetion
+
+