Вопрос

I have a small script that uses mechanize to go to a few websites to collect data. Sometimes the websites cause the script the crash and returns either "Bad Gateway" or "Connection Reset By Peer". I have an except line in the code so it emails me if there are any problems with the program, but I am trying to find a way to have it NOT email me in the case of these 2 problems. I've tried:

except Exception as e:
    if 'Gateway' not in e and 'peer' not in e:
        logger.error(e)

But this doesn't seem to work, as i still get emails if one of these 2 errors occur. What is the best way to have it email me in the case of any exception except for exceptions that contain certain text?

Это было полезно?

Решение

Instead of e in your if statement, use str(e).

except Exception as e:
    if 'Gateway' not in str(e) and 'peer' not in str(e):
        logger.error(e)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top