-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.py
54 lines (47 loc) · 1.32 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#
# twitterbot2
#
# edoardottt
# edoardoottavianelli.it
# https://github.com/edoardottt/twitterbot2
#
# This repository is under GPL-3 License.
#
#
# This file contains the logic when a new TwitterHTTPError
# happens.
#
import time
import logging
import twitter
import urllib
def error_handler(e):
"""
This function handles all the twitterbot2 errors
- twitter.api.TwitterHTTPError
- urllib.error.URLError
- generic
"""
logger = logging.getLogger("__main__")
if e.__class__ == twitter.api.TwitterHTTPError:
logger.error(str(e.e) + " on " + e.uri)
# == 429 TOO MANY REQUESTS -> Sleep for one hour
if str(e.e.code) == "429":
logger.info("Sleeping for one hour.")
time.sleep(60 * 60)
return
# == 403 FORBIDDEN -> Sleep for ten seconds
if str(e.e.code) == "403":
logger.info("Sleeping for ten seconds.")
time.sleep(10)
return
elif e.__class__ == urllib.error.URLError:
logger.error(str(e.reason))
logger.info("Sleeping for five minutes.")
time.sleep(5 * 60)
# for other types of issues with specific behaviour
# add here an additional handler
# == DEFAULT ==
else:
logger.info("Sleeping for ten seconds.")
time.sleep(10)