Properly handle HTTP stream errors

This commit is contained in:
Zeva Rose 2022-06-14 09:00:53 -06:00
parent f5a8312d78
commit 969999fc46

View file

@ -9,7 +9,6 @@ from jarvis_core.db.models import TwitterAccount, TwitterFollow
from naff import Client
from naff.client.errors import NotFound
from naff.models.discord.embed import Embed
from tweepy import errors
from tweepy.asynchronous import AsyncStream
from tweepy.models import Status
@ -89,22 +88,22 @@ class JARVISTwitterStream(AsyncStream):
async def on_keep_alive(self) -> None:
"""Override keep-alive to track new accounts."""
try:
await super().on_keep_alive()
except errors.Unauthorized as e:
logger.error("Got 401, killing", stack_info=True)
logger.debug(f"{e.api_errors=}")
logger.debug(f"{e.api_messages=}")
self.disconnect()
except Exception:
logger.error("Encountered error with stream in keepalive", stack_info=True)
self.disconnect()
else:
ids = [x.twitter_id async for x in TwitterAccount.find()]
if ids != self.current_filter:
logger.debug("Follows have changed, disconnected")
self.disconnect()
async def on_request_error(self, status_code: int) -> None:
"""
Wrapper for on_request_error. Mainly used to catch 401 errors.
Args:
status_code: HTTP Status Code
"""
logger.error(f"Received status code {status_code} while streaming, restarting")
self.disconnect()
async def on_status(self, status: Status) -> None:
"""
Process new statuses.