-
-
Notifications
You must be signed in to change notification settings - Fork 172
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
Create Tweet Errors #202
base: main
Are you sure you want to change the base?
Create Tweet Errors #202
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,6 +33,8 @@ | |||||||||||||||||||||
Unauthorized, | ||||||||||||||||||||||
UserNotFound, | ||||||||||||||||||||||
UserUnavailable, | ||||||||||||||||||||||
CreateTweetDuplicate, | ||||||||||||||||||||||
CreateTweetMaxLengthReached, | ||||||||||||||||||||||
raise_exceptions_from_response | ||||||||||||||||||||||
) | ||||||||||||||||||||||
from ..geo import Place, _places_from_response | ||||||||||||||||||||||
|
@@ -1238,6 +1240,8 @@ async def create_tweet( | |||||||||||||||||||||
reply_to, attachment_url, community_id, share_with_followers, | ||||||||||||||||||||||
richtext_options, edit_tweet_id, limit_mode | ||||||||||||||||||||||
) | ||||||||||||||||||||||
if not response['data']: | ||||||||||||||||||||||
self._switch_error(response) | ||||||||||||||||||||||
if is_note_tweet: | ||||||||||||||||||||||
_result = response['data']['notetweet_create']['tweet_results'] | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
|
@@ -4237,3 +4241,21 @@ async def _update_subscriptions( | |||||||||||||||||||||
async def _get_user_state(self) -> Literal['normal', 'bounced', 'suspended']: | ||||||||||||||||||||||
response, _ = await self.v11.user_state() | ||||||||||||||||||||||
return response['userState'] | ||||||||||||||||||||||
|
||||||||||||||||||||||
def _switch_error( | ||||||||||||||||||||||
self, | ||||||||||||||||||||||
response: dict | ||||||||||||||||||||||
): | ||||||||||||||||||||||
error_map = { | ||||||||||||||||||||||
186: CreateTweetDuplicate, | ||||||||||||||||||||||
187: CreateTweetMaxLengthReached | ||||||||||||||||||||||
} | ||||||||||||||||||||||
message = response.get('errors', [])[0].get('message', '') | ||||||||||||||||||||||
error_msg: str = message[message.index(": ")+2:message.index(". ")] | ||||||||||||||||||||||
|
||||||||||||||||||||||
Comment on lines
+4253
to
+4255
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve error message parsing to prevent exceptions Extracting Apply this diff to make error parsing more robust: message = response.get('errors', [])[0].get('message', '')
-error_msg: str = message[message.index(": ")+2:message.index(". ")]
+colon_index = message.find(": ")
+period_index = message.find(". ")
+if colon_index != -1 and period_index != -1 and colon_index < period_index:
+ error_msg = message[colon_index + 2 : period_index]
+else:
+ error_msg = message 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||
error_code = response.get('errors', [])[0].get('extensions', {}).get('code', '') | ||||||||||||||||||||||
if error_code in error_map: | ||||||||||||||||||||||
raise error_map[error_code](error_msg) | ||||||||||||||||||||||
Comment on lines
+4253
to
+4258
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check for empty 'errors' list in response to avoid IndexError The code assumes that Apply this diff to safely access the error information: -errors = response.get('errors', [])[0]
+errors = response.get('errors', [])
+if not errors:
+ # Handle case where errors list is empty
+ return
+error_info = errors[0]
-message = response.get('errors', [])[0].get('message', '')
+message = error_info.get('message', '')
+error_code = error_info.get('extensions', {}).get('code', '')
Comment on lines
+4256
to
+4258
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure error codes are consistently typed for correct mapping The Apply this diff to ensure consistent data types: error_map = {
- 186: CreateTweetDuplicate,
- 187: CreateTweetMaxLengthReached
+ '186': CreateTweetDuplicate,
+ '187': CreateTweetMaxLengthReached
}
error_code = response.get('errors', [])[0].get('extensions', {}).get('code', '')
|
||||||||||||||||||||||
# * Print <response> to reach unknown errors in 2024-09-04 | ||||||||||||||||||||||
print(response) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Replace print with logging for unknown errors Using
|
||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Consider refactoring error handling logic
The current implementation of
_switch_error
mixes different responsibilities. Consider using a dictionary to map error messages to exception types, which would make it easier to maintain and extend in the future.