Update twitter error handling
This commit is contained in:
parent
f5b708aafe
commit
67cc454595
2 changed files with 57 additions and 23 deletions
|
|
@ -193,3 +193,35 @@ class TwitterClientTestCase(TestCase):
|
||||||
|
|
||||||
self.assertIsNone(user.twitter_oauth_token)
|
self.assertIsNone(user.twitter_oauth_token)
|
||||||
self.assertIsNone(user.twitter_oauth_token_secret)
|
self.assertIsNone(user.twitter_oauth_token_secret)
|
||||||
|
|
||||||
|
def test_client_does_not_reset_token(self):
|
||||||
|
"""
|
||||||
|
The user's token and refresh token should not be reset when an generic
|
||||||
|
exception is caught
|
||||||
|
"""
|
||||||
|
user = UserFactory(
|
||||||
|
twitter_oauth_token=str(uuid4()), twitter_oauth_token_secret=str(uuid4())
|
||||||
|
)
|
||||||
|
timeline = TwitterTimelineFactory(user=user)
|
||||||
|
|
||||||
|
response = Mock(json=lambda: {"errors": [{"code": 100}]})
|
||||||
|
|
||||||
|
self.mocked_read.side_effect = StreamException(
|
||||||
|
message="Generic message", response=response
|
||||||
|
)
|
||||||
|
|
||||||
|
with TwitterClient([timeline]) as client:
|
||||||
|
for data, stream in client:
|
||||||
|
with self.subTest(data=data, stream=stream):
|
||||||
|
self.assertIsNone(data)
|
||||||
|
self.assertIsNone(stream)
|
||||||
|
self.assertEquals(stream.rule.error, "")
|
||||||
|
self.assertEquals(stream.rule.succeeded, False)
|
||||||
|
|
||||||
|
self.mocked_read.assert_called()
|
||||||
|
|
||||||
|
user.refresh_from_db()
|
||||||
|
timeline.refresh_from_db()
|
||||||
|
|
||||||
|
self.assertIsNotNone(user.twitter_oauth_token)
|
||||||
|
self.assertIsNotNone(user.twitter_oauth_token_secret)
|
||||||
|
|
|
||||||
|
|
@ -250,39 +250,41 @@ class TwitterClient(PostClient):
|
||||||
try:
|
try:
|
||||||
response_data = e.response.json()
|
response_data = e.response.json()
|
||||||
except JSONDecodeError:
|
except JSONDecodeError:
|
||||||
|
logger.exception("Could not parse json for request")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if "errors" in response_data:
|
if "errors" in response_data:
|
||||||
errors = response_data["errors"]
|
errors = response_data["errors"]
|
||||||
token_expired = any(error["code"] == 89 for error in errors)
|
token_expired = any(error["code"] == 89 for error in errors)
|
||||||
|
|
||||||
try:
|
if token_expired:
|
||||||
import sentry_sdk
|
try:
|
||||||
|
import sentry_sdk
|
||||||
|
|
||||||
with sentry_sdk.push_scope() as scope:
|
with sentry_sdk.push_scope() as scope:
|
||||||
scope.set_extra("content", response_data)
|
scope.set_extra("content", response_data)
|
||||||
sentry_sdk.capture_message(
|
sentry_sdk.capture_message(
|
||||||
"Twitter authentication credentials reset"
|
"Twitter authentication credentials reset"
|
||||||
)
|
)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
stream.rule.user.twitter_oauth_token = None
|
stream.rule.user.twitter_oauth_token = None
|
||||||
stream.rule.user.twitter_oauth_token_secret = None
|
stream.rule.user.twitter_oauth_token_secret = None
|
||||||
stream.rule.user.save()
|
stream.rule.user.save()
|
||||||
|
|
||||||
message = _(
|
message = _(
|
||||||
"Your Twitter account credentials have expired. Re-authenticate in"
|
"Your Twitter account credentials have expired. Re-authenticate in"
|
||||||
" the settings page to keep retrieving Twitter specific information"
|
" the settings page to keep retrieving Twitter specific information"
|
||||||
" from your account."
|
" from your account."
|
||||||
)
|
)
|
||||||
|
|
||||||
send_mail(
|
send_mail(
|
||||||
"Twitter account needs re-authentication",
|
"Twitter account needs re-authentication",
|
||||||
message,
|
message,
|
||||||
None,
|
None,
|
||||||
[stream.rule.user.email],
|
[stream.rule.user.email],
|
||||||
)
|
)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
finally:
|
finally:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue