Handle "error" key in response JSON when initializing HTTPException
authorHarmon <Harmon758@gmail.com>
Wed, 9 Jun 2021 17:51:52 +0000 (12:51 -0500)
committerHarmon <Harmon758@gmail.com>
Wed, 9 Jun 2021 17:51:52 +0000 (12:51 -0500)
tweepy/errors.py

index d7b0767de75a0ed69c9ec7a511c240a8f7fe79ed..fe595ec1386fc7cd4c06cf31db4cab273ca65f9a 100644 (file)
@@ -25,24 +25,23 @@ class HTTPException(TweepyException):
         except json.JSONDecodeError:
             super().__init__(f"{response.status_code} {response.reason}")
         else:
-            error_text = []
+            errors = response_json.get("errors", [])
             # Use := when support for Python 3.7 is dropped
-            if "errors" not in response_json:
-                super().__init__(f"{response.status_code} {response.reason}")
-                return
-            for error in response_json["errors"]:
+            if "error" in response_json:
+                errors.append(response_json["error"])
+            error_text = ""
+            for error in errors:
                 self.api_errors.append(error)
                 if "code" in error:
                     self.api_codes.append(error["code"])
                 if "message" in error:
                     self.api_messages.append(error["message"])
                 if "code" in error and "message" in error:
-                    error_text.append(f"{error['code']} - {error['message']}")
+                    error_text += f"\n{error['code']} - {error['message']}"
                 elif "message" in error:
-                    error_text.append(error["message"])
-            error_text = '\n'.join(error_text)
+                    error_text += '\n' + error["message"]
             super().__init__(
-                f"{response.status_code} {response.reason}\n{error_text}"
+                f"{response.status_code} {response.reason}{error_text}"
             )