[mousetrap/gnome3-wip: 217/240] Chain exceptions for FeatureNotFoundException on cache hits.



commit 7704ea9e008b12bfeff9d1f4ddc1377a20c63544
Author: Stoney Jackson <dr stoney gmail com>
Date:   Mon Jun 30 09:44:55 2014 -0400

    Chain exceptions for FeatureNotFoundException on cache hits.
    
    Clients can now retreive the original exception that caused the
    new exception as follows
    
    ```
    try:
        detector.detect(image)
    except FeatureNotFoundException as exception:
        print exception    # message contains information about cause too.
        if exception.cause is not None:
            print exception.cause
    ```

 src/mousetrap/vision.py |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
---
diff --git a/src/mousetrap/vision.py b/src/mousetrap/vision.py
index 5fddc7b..0b69262 100644
--- a/src/mousetrap/vision.py
+++ b/src/mousetrap/vision.py
@@ -134,7 +134,9 @@ class FeatureDetector(object):
                     {'image':id(image), 'result':self._detect_cache[image]}
                     )
             if isinstance(self._detect_cache[image], FeatureNotFoundException):
-                raise FeatureNotFoundException(str(self._detect_cache[image]))
+                message = str(self._detect_cache[image])
+                raise FeatureNotFoundException(message,
+                        cause=self._detect_cache[image])
             return self._detect_cache[image]
         try:
             self._image = image
@@ -203,4 +205,7 @@ class FeatureDetectorClearCachePlugin(interface.Plugin):
 
 
 class FeatureNotFoundException(Exception):
-    pass
+    def __init__(self, message, cause = None):
+        if cause is not None:
+            message = message + ', caused by ' + repr(cause)
+        self.cause = cause


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]