This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hey all. Just wanted to share a very basic error handling format that restarts a script after a delay if there is a Reddit-side server issue for loops involving things like sub.stream.submissions() and sub.stream.comments().
I've heard from a few people new to bot scripting that their continuous uptime worker scripts will stop running due to persistent Reddit-side server issues they can't get around. This format has worked for me for a while, hopefully it helps a few others.
import prawcore.exceptions
import time
... *other packages*
while True:
try:
for submission in subreddit.stream.submissions():
# do something with submission ...
except prawcore.exceptions.ServerError as e:
print(f"A Reddit server error occurred: [{e}]. Sleeping before trying again...")
time.sleep(60)
pass
except Exception as e:
print(f'An unknown error occurred: [{e}]')
raise
Sample Output:
A Reddit server error occurred: [received 502 HTTP response]. Sleeping before trying again...
This additionally works for any other non-critical error that you would like to trigger a restart of the script. You would just replace prawcore.exceptions.ErrorName with the correct error name you would like to consider.
If anyone has a more elegant way to error handling in their experience, let me know.
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/redditdev/c...