Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More comprehensive error handling on tweet creation #261

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions twikit/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,11 @@ async def create_tweet(
reply_to, attachment_url, community_id, share_with_followers,
richtext_options, edit_tweet_id, limit_mode
)
if 'errors' in response.keys():
raise_exceptions_from_response(response['errors'])
raise CouldNotTweet(
response['errors'][0] if response['errors'] else 'Failed to post a tweet.'
)
Comment on lines +1241 to +1245
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Simplify error handling logic and fix potential KeyError

The current error handling has redundant logic and could raise a KeyError if response['errors'] is empty. Consider simplifying the logic and adding a safe default.

-if 'errors' in response.keys():
-    raise_exceptions_from_response(response['errors'])
-    raise CouldNotTweet(
-        response['errors'][0] if response['errors'] else 'Failed to post a tweet.'
-    )
+if 'errors' in response:
+    errors = response['errors']
+    raise_exceptions_from_response(errors)
+    error_message = errors[0]['message'] if errors else 'Failed to post a tweet.'
+    raise CouldNotTweet(error_message)

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Ruff (0.7.0)

1241-1241: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

if is_note_tweet:
_result = response['data']['notetweet_create']['tweet_results']
else:
Expand Down