Working around a bug for some and providing more helpful error message for others
[theonering] / src / gvoice / backend.py
index 7950c65..9151954 100755 (executable)
@@ -697,7 +697,11 @@ class GVoiceBackend(object):
                        message = Message()
                        message.body = messageParts
                        message.whoFrom = conv.name
-                       message.when = conv.time.strftime("%I:%M %p")
+                       try:
+                               message.when = conv.time.strftime("%I:%M %p")
+                       except ValueError:
+                               _moduleLogger.exception("Confusing time provided: %r" % conv.time)
+                               message.when = "Unknown"
                        conv.messages = (message, )
 
                        yield conv
@@ -787,9 +791,27 @@ class GVoiceBackend(object):
 
        def _parse_with_validation(self, page):
                json = parse_json(page)
-               validate_response(json)
+               self._validate_response(json)
                return json
 
+       def _validate_response(self, response):
+               """
+               Validates that the JSON response is A-OK
+               """
+               try:
+                       assert response is not None, "Response not provided"
+                       assert 'ok' in response, "Response lacks status"
+                       assert response['ok'], "Response not good"
+               except AssertionError:
+                       try:
+                               if response["data"]["code"] == 20:
+                                       raise RuntimeError(
+"""Ambiguous error 20 returned by Google Voice.
+Please verify you have configured your callback number (currently "%s").  If it is configured some other suspected causes are: non-verified callback numbers, and Gizmo5 callback numbers.""" % self._callbackNumber)
+                       except KeyError:
+                               pass
+                       raise RuntimeError('There was a problem with GV: %s' % response)
+
 
 _UNESCAPE_ENTITIES = {
  """: '"',
@@ -891,18 +913,6 @@ def extract_payload(flatXml):
        return jsonTree, flatHtml
 
 
-def validate_response(response):
-       """
-       Validates that the JSON response is A-OK
-       """
-       try:
-               assert response is not None
-               assert 'ok' in response
-               assert response['ok']
-       except AssertionError:
-               raise RuntimeError('There was a problem with GV: %s' % response)
-
-
 def guess_phone_type(number):
        if number.startswith("747") or number.startswith("1747") or number.startswith("+1747"):
                return GVoiceBackend.PHONE_TYPE_GIZMO