Repository: dmwm/wmcore
Issue: Another unicode encoding failure

It is in

https://github.com/dmwm/WMCore/blob/master/src/python/WMCore/Storage/Execute.py#L70

output contains "\\x95" which cause encoding error
  
and

'utf8' codec can't decode byte 0xb2 in position 24451


Changed files:
src/python/WMCore/FwkJobReport/Report.py, src/python/WMCore/WMException.py

diff --git a/src/python/WMCore/FwkJobReport/Report.py b/src/python/WMCore/FwkJobReport/Report.py
index 06e93c611..09ca0e704 100644
--- a/src/python/WMCore/FwkJobReport/Report.py
+++ b/src/python/WMCore/FwkJobReport/Report.py
@@ -592,7 +592,9 @@ class Report(object):
             # interprets this string using utf-8 codec and ignoring any errors
             errDetails.details = errorDetails.decode('utf-8', 'ignore')
         else:
-            errDetails.details = errorDetails
+            # Then cast it to string and decode it
+            errorDetails = str(errorDetails)
+            errDetails.details = errorDetails.decode('utf-8', 'ignore')
 
         setattr(stepSection.errors, "errorCount", errorCount + 1)
         self.setStepStatus(stepName=stepName, status=exitCode)
diff --git a/src/python/WMCore/WMException.py b/src/python/WMCore/WMException.py
index 36bf35dd6..591913a41 100644
--- a/src/python/WMCore/WMException.py
+++ b/src/python/WMCore/WMException.py
@@ -9,9 +9,8 @@ General Exception class for WM modules
 import exceptions
 import inspect
 import logging
-import traceback
 import sys
-import re
+import traceback
 
 WMEXCEPTION_START_STR = "<@========== WMException Start ==========@>"
 WMEXCEPTION_END_STR = "<@---------- WMException End ----------@>"
@@ -25,6 +24,7 @@ class WMException(exceptions.Exception):
     it was raised.
 
     """
+
     def __init__(self, message, errorNo=None, **data):
         self.name = str(self.__class__.__name__)
         if hasattr(message, "decode"):
@@ -66,7 +66,7 @@ class WMException(exceptions.Exception):
             finally:
                 frame = None
 
-        #  //
+        # //
         # // Find out where the exception came from
         # //
         try:
@@ -77,21 +77,19 @@ class WMException(exceptions.Exception):
         finally:
             stack = None
 
-        #  //
+        # //
         # // ClassName if ClassInstance is passed
         # //
         try:
             if self.data['ClassInstance'] != None:
-                self.data['ClassName'] = \\
-                      self.data['ClassInstance'].__class__.__name__
-        except:
+                self.data['ClassName'] = self.data['ClassInstance'].__class__.__name__
+        except Exception:
             pass
 
-
         # Determine the traceback at time of __init__
         try:
             self.traceback = "\\n".join(traceback.format_tb(sys.exc_info()[2]))
-        except:
+        except Exception:
             self.traceback = "WMException error: Couldn't get traceback\\n"
 
     def __getitem__(self, key):
@@ -153,6 +151,9 @@ class WMException(exceptions.Exception):
         strg += self.traceback
         strg += '\\n'
         strg += WMEXCEPTION_END_STR
+        if hasattr(strg, "decode"):
+            # Fix for the unicode encoding issue, #8043
+            strg = strg.decode('utf-8', 'ignore')
         return strg
 
     def message(self):