From 39d5f1d77abda08a5a5483c5b774287775b2318c Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Wed, 5 Apr 2017 18:45:06 +0100
Subject: [PATCH] Error message can be suppressed with reportIfError context
 manager

---
 fsl/utils/status.py | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/fsl/utils/status.py b/fsl/utils/status.py
index e52e9b37c..ee40e787a 100644
--- a/fsl/utils/status.py
+++ b/fsl/utils/status.py
@@ -139,9 +139,15 @@ def reportError(title, msg, err):
     
 
 @contextlib.contextmanager
-def reportIfError(title, msg, raiseError=True):
+def reportIfError(title, msg, raiseError=True, report=True):
     """A context manager which calls :func:`reportError` if the enclosed code
     raises an ``Exception``.
+
+    :arg raiseError: If ``True``, the ``Exception`` which was raised is
+                     propagated upwards.
+
+    :arg report:     Defaults to ``True``. If ``False``, an error message
+                     is logged, but :func:`reportError` is not called.
     """
     try:
         yield
@@ -150,21 +156,22 @@ def reportIfError(title, msg, raiseError=True):
 
         log.error('{}: {}'.format(title, msg), exc_info=True)
 
-        reportError(title, msg, e)
+        if report:
+            reportError(title, msg, e)
 
         if raiseError:
             raise
 
 
-def reportErrorDecorator(title, msg):
-    """A decorator which calls :func:`reportError` if the decorated function
-    raises an ``Exception``.
+def reportErrorDecorator(*args, **kwargs):
+    """A decorator which wraps the decorated function with
+    :func:`reportIfError`.
     """ 
 
     def decorator(func):
-        def wrapper(*args, **kwargs):
-            with reportIfError(title, msg):
-                func(*args, **kwargs)
+        def wrapper(*wargs, **wkwargs):
+            with reportIfError(*args, **kwargs):
+                func(*wargs, **wkwargs)
 
         return wrapper
 
-- 
GitLab