Catch exceptions from posting to Twitter

This change updates Statusbot to catch exceptions from posting to
Twitter. This fixes an issue where Statusbot crashes if Twitter returns
a non-OK status from a post. An example can be found here [1].

http://paste.openstack.org/show/602748/

Change-Id: Iae3ad57e2e205b60a730ddffe8cba0f68d1f99bb
This commit is contained in:
Ben Kero 2017-03-14 14:38:33 -07:00
parent 57d6ed9ad7
commit 3bb38cc92b
1 changed files with 12 additions and 3 deletions

View File

@ -167,17 +167,26 @@ class Tweet(UpdateInterface):
consumer_secret=self.consumer_secret,
access_token_key=self.access_token_key,
access_token_secret=self.access_token_secret)
self.logger = logging.getLogger("statusbot.bot")
def update(self, msg):
# Limit tweets to 120 characters to facilitate retweets
tweets = textwrap.wrap(msg, 120)
if len(tweets) == 1:
# Don't prefix statuses that fit
self.api.PostUpdate(tweets[0])
try:
self.api.PostUpdate(tweets[0])
except twitter.TwitterError:
self.logger.exception("Error encountered posting"
"to Twitter: %s" % msg)
else:
for index in range(0, len(tweets)):
self.api.PostUpdate("{index}/{tweet}".format(
index=index, tweet=tweets[index]))
try:
self.api.PostUpdate("{index}/{tweet}".format(
index=index, tweet=tweets[index]))
except twitter.TwitterError:
self.logger.exception("Error encountered posting"
"to Twitter: %s" % msg)
def alert(self, msg=None):
self.update(msg)