0

I have a list of twitter usernames that I use to get tweets. During the process, I face 2 main errors:

CRITICAL:root:twint.get:User:'user'
raise ValueError("Cannot find twitter account with name = " + self.config.Username)
ValueError: Cannot find twitter account with name = blablabla

CRITICAL:root:twint.get:User:'data'
raise ValueError("Cannot find twitter account with name = " + self.config.Username)
ValueError: Cannot find twitter account with name = blablabla

The first one happens when the username is incorrect and I don't mind passing the error. However, the second one I believe is due to connection timeout or something similar, because whenever I get that error, I stop the script for 3-4 minutes and continue and it goes on as usual.

Therefore, I want to catch the 2nd error and handle it with sleep(). The problem is, if I do this it will sleep through incorrect username error as well and that's a waste of time:

try:
    some code
except ValueError:
    sleep(200)

How can I make it sleep only if the 2nd error happens?

Musa G.
  • 23
  • 2
  • You need to use a counter to know how many times exception arose. – ThePyGuy Feb 26 '21 at 15:06
  • If they're both the same exact error class, you'll need to do something like catch it, then check the value of the exception using `str.startswith` or something. Not pretty, but I had to do something similar in Haskell to handle different socket errors. You could also modify the library to instead throw custom subclasses of `ValueError`, then catch the subclasses, but that's a bit messy. – Carcigenicate Feb 26 '21 at 15:07

0 Answers0