Skip to content
Advertisement

How to handle an error and make it a result

I’m checking whether the TikTok live video is (live now) or (ended).

Since it is hard to deal with TikTok a bit, I will make a sign and rely on it.

The following code will take the TikToker username. If there is a live video running, it will show the viewers count, so we decide that it is (live now). Otherwise (e.g. in the code below), it is going to be (ended). As simple as that.

from TikTokLive import TikTokLiveClient
from TikTokLive.types.events import ViewerCountUpdateEvent

client: TikTokLiveClient = TikTokLiveClient(unique_id="8_o2o", **({"fetch_room_info_on_connect": True}))


@client.on("viewer_count_update")
async def on_connect(event: ViewerCountUpdateEvent):
    print("Received a new viewer count:", event.viewerCount)


if __name__ == '__main__':
    client.run()

When the live is ended, it will raise an error that says:

Traceback (most recent call last):
  File "C:ProgramDataAnaconda3envspracticelibsite-packagesTikTokLiveclientbase.py", line 216, in _connect
    raise LiveNotFound()
TikTokLive.types.errors.LiveNotFound

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/ahmad/Desktop/ExTrac_Chat_DB/tiktok.py", line 13, in <module>
    client.run()
  File "C:ProgramDataAnaconda3envspracticelibsite-packagesTikTokLiveclientbase.py", line 293, in run
    self.loop.run_until_complete(self._connect())
  File "C:ProgramDataAnaconda3envspracticelibasynciobase_events.py", line 616, in run_until_complete
    return future.result()
  File "C:ProgramDataAnaconda3envspracticelibsite-packagesTikTokLiveclientclient.py", line 38, in _connect
    result: str = await super(TikTokLiveClient, self)._connect()
  File "C:ProgramDataAnaconda3envspracticelibsite-packagesTikTokLiveclientbase.py", line 244, in _connect
    raise FailedConnection(message)
TikTokLive.types.errors.FailedConnection

What I need is to handle that error. It’s not really an error, I just want it to be a result for me. I can’t deal with it.

Advertisement

Answer

You need to catch the LiveNotFound exception and handle it manually. You can do this by wrapping your client.run() call into a try-except block like this:

from TikTokLive import TikTokLiveClient
from TikTokLive.types.events import ViewerCountUpdateEvent
from TikTokLive.types.errors import LiveNotFound

client: TikTokLiveClient = TikTokLiveClient(unique_id="8_o2o", **({"fetch_room_info_on_connect": True}))


@client.on("viewer_count_update")
async def on_connect(event: ViewerCountUpdateEvent):
    print("Received a new viewer count:", event.viewerCount)


if __name__ == '__main__':
    try:
        client.run()
    except LiveNotFound:
        print("The video is no longer live.")

Generally, it is good practice to at least log any exception that happens, so that you know what is happening in your code. Also note that the LiveNotFound exception could be possibly thrown also in other cases (for example if there is some connection issue), and your code might not be able to distinguish between those cases.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement