Error Handling
Guide to handling common errors and exceptions in RedHarmony.
Common Errors
Reddit API Errors
- Rate Limiting
try:
create_post(reddit, username, subreddit, title, content)
except praw.exceptions.APIException as e:
if e.error_type == "RATELIMIT":
# Wait and retry
time.sleep(600) # 10 minutes
- Authentication Errors
try:
reddit = get_reddit_client(account)
except prawcore.exceptions.OAuthException:
logger.error("Authentication failed - check credentials")
OpenAI API Errors
- Token Limits
try:
response = openAI_generate(prompt, max_tokens)
except openai.error.InvalidRequestError as e:
if "maximum context length" in str(e):
# Reduce prompt length or max_tokens
return openAI_generate(prompt[:1000], max_tokens//2)
- Rate Limits
try:
generate_content()
except openai.error.RateLimitError:
time.sleep(60) # Wait 1 minute
retry_with_backoff()
Database Errors
Connection Issues
def get_db_connection_with_retry():
retries = 3
while retries > 0:
try:
return sqlite3.connect("reddit_bot.db")
except sqlite3.Error:
retries -= 1
time.sleep(1)
raise Exception("Could not connect to database")
Data Integrity
def safe_save_post(post_id, username, subreddit, title):
try:
save_post(post_id, username, subreddit, title)
except sqlite3.IntegrityError:
# Handle duplicate post
logger.warning(f"Duplicate post: {post_id}")