From e1c154809aa927ea2865fc978da3993d0d812e96 Mon Sep 17 00:00:00 2001
From: Amir MOHAMMADI <amir.mohammadi@idiap.ch>
Date: Fri, 9 Mar 2018 17:12:42 +0100
Subject: [PATCH] improve error handling

---
 bob/bio/base/annotator/FailSafe.py   |  2 +-
 bob/bio/base/script/annotate.py      |  4 ++--
 bob/bio/base/test/dummy/annotator.py | 18 ++++++++++++------
 bob/bio/base/test/test_annotators.py |  8 +++++++-
 4 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/bob/bio/base/annotator/FailSafe.py b/bob/bio/base/annotator/FailSafe.py
index caa57719..4ba32ad6 100644
--- a/bob/bio/base/annotator/FailSafe.py
+++ b/bob/bio/base/annotator/FailSafe.py
@@ -26,7 +26,7 @@ class FailSafe(Annotator):
 
     def annotate(self, sample, **kwargs):
         if 'annotations' not in kwargs or kwargs['annotations'] is None:
-            kwargs['annotations'] = None
+            kwargs['annotations'] = {}
         for annotator in self.annotators:
             try:
                 annotations = annotator(sample, **kwargs)
diff --git a/bob/bio/base/script/annotate.py b/bob/bio/base/script/annotate.py
index 4df5c528..e9dc135a 100644
--- a/bob/bio/base/script/annotate.py
+++ b/bob/bio/base/script/annotate.py
@@ -77,9 +77,9 @@ def annotate(database, annotator, output_dir, force, array, **kwargs):
         outpath = biofile.make_path(output_dir, '.json')
         if isfile(outpath):
             if force:
-                logger.debug("Overwriting the annotations file `%s'", outpath)
+                logger.info("Overwriting the annotations file `%s'", outpath)
             else:
-                logger.debug("The annotation `%s' already exists", outpath)
+                logger.info("The annotation `%s' already exists", outpath)
                 continue
 
         logger.info(
diff --git a/bob/bio/base/test/dummy/annotator.py b/bob/bio/base/test/dummy/annotator.py
index e58f6c62..0a780fee 100644
--- a/bob/bio/base/test/dummy/annotator.py
+++ b/bob/bio/base/test/dummy/annotator.py
@@ -1,11 +1,17 @@
+from random import random
 from bob.bio.base.annotator import FailSafe, Callable
 
 
-def simple_annotator(image, **kwargs):
-    return {
-        'topleft': (0, 0),
-        'bottomright': image.shape,
-    }
+def moody_annotator(image, **kwargs):
+    if random() < 0.5:
+        return {
+            'topleft': (0, 0),
+        }
+    else:
+        return {
+            'topleft': (0, 0),
+            'bottomright': image.shape,
+        }
 
 
 def fail_annotator(image, **kwargs):
@@ -14,6 +20,6 @@ def fail_annotator(image, **kwargs):
 
 annotator = FailSafe(
     [Callable(fail_annotator),
-     Callable(simple_annotator)],
+     Callable(moody_annotator)],
     required_keys=['topleft', 'bottomright'],
 )
diff --git a/bob/bio/base/test/test_annotators.py b/bob/bio/base/test/test_annotators.py
index ee4cdc77..b8735b1e 100644
--- a/bob/bio/base/test/test_annotators.py
+++ b/bob/bio/base/test/test_annotators.py
@@ -13,7 +13,13 @@ def test_annotate():
         runner = CliRunner()
         result = runner.invoke(annotate, args=(
             '-d', 'dummy', '-a', 'dummy', '-o', tmp_dir))
-        assert result.exit_code == 0, result.output
+        assertion_error_message = (
+            'Command exited with this output: `{}\' \n'
+            'If the output is empty, you can run this script locally to see '
+            'what is wrong:\n'
+            'bin/bob bio annotate -vvv --force -d dummy -a dummy -o /tmp/temp_annotations'
+            ''.format(result.output))
+        assert result.exit_code == 0, assertion_error_message
 
         # test if annotations exist
         for dirpath, dirnames, filenames in os.walk(tmp_dir):
-- 
GitLab