Twitter Error display bug in Python 3
[rainbowstream.git] / rainbowstream / util.py
index 232845c031e807709fb368e3142ce5d91cd72bc8..e8d3c74c0b4ba454ffc7fa4cf6c28824477bd66b 100644 (file)
@@ -1,16 +1,20 @@
 import json
 
 from twitter.util import printNicely
+from .colors import *
 
-from .colors import magenta
 
-
-def printTwitterErrors(twitterException):
+def detail_twitter_error(twitterException):
+    """
+    Display Twitter Errors nicely
+    """
     try:
-        loadedJson = json.loads(twitterException.response_data)
+        # twitterException.response_data can be byte string on Python 3
+        # or nornal dict on Python 2
+        loadedJson = json.loads(twitterException.response_data.decode('utf8'))
         for m in loadedJson.get('errors', dict()):
-            printNicely(
-                magenta("Error " + str(m.get('code')) + ": " + m.get('message')))
-    except ValueError:
-        printNicely(
-                magenta( "Error: " + twitterException.response_data))
+            info = "Error " + str(m.get('code')) + ": " + m.get('message')
+            printNicely(yellow(info))
+    except:
+        info = "Error: " + twitterException.response_data.decode('utf8')
+        printNicely(yellow(info))