Added logging and error handling for posts and comments.
This commit is contained in:
@@ -261,7 +261,7 @@ class SubredditContent(BaseContent):
|
||||
display_name += '/{}'.format(order)
|
||||
|
||||
if name == 'me':
|
||||
if not self.reddit.is_logged_in():
|
||||
if not reddit.is_logged_in():
|
||||
raise AccountError
|
||||
else:
|
||||
submissions = reddit.user.get_submitted(sort=order)
|
||||
|
||||
@@ -244,11 +244,12 @@ class BasePage(object):
|
||||
return
|
||||
|
||||
try:
|
||||
with self.loader():
|
||||
self.reddit.login(username, password)
|
||||
except praw.errors.InvalidUserPass:
|
||||
show_notification(self.stdscr, ['Invalid user/pass'])
|
||||
else:
|
||||
show_notification(self.stdscr, ['Logged in'])
|
||||
show_notification(self.stdscr, ['Welcome {}'.format(username)])
|
||||
|
||||
def logout(self):
|
||||
"Prompt to log out of the user's account."
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import curses
|
||||
import sys
|
||||
import time
|
||||
import logging
|
||||
|
||||
import praw.errors
|
||||
|
||||
@@ -13,6 +14,7 @@ from .docs import COMMENT_FILE
|
||||
|
||||
__all__ = ['SubmissionController', 'SubmissionPage']
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
class SubmissionController(BaseController):
|
||||
character_map = {}
|
||||
@@ -87,7 +89,7 @@ class SubmissionPage(BasePage):
|
||||
"""
|
||||
|
||||
if not self.reddit.is_logged_in():
|
||||
show_notification(self.stdscr, ['Not logged in'])
|
||||
show_notification(self.stdscr, ['Login to post'])
|
||||
return
|
||||
|
||||
data = self.content.get(self.nav.absolute_index)
|
||||
@@ -110,7 +112,7 @@ class SubmissionPage(BasePage):
|
||||
comment_text = open_editor(comment_info)
|
||||
curses.doupdate()
|
||||
if not comment_text:
|
||||
curses.flash()
|
||||
show_notification(self.stdscr, ['Comment canceled'])
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -119,8 +121,14 @@ class SubmissionPage(BasePage):
|
||||
else:
|
||||
data['object'].reply(comment_text)
|
||||
except praw.errors.APIException:
|
||||
curses.flash()
|
||||
message = ['Error: {}'.format(e.error_type), e.message]
|
||||
show_notification(self.stdscr, message)
|
||||
_logger.exception(e)
|
||||
except requests.HTTPError as e:
|
||||
show_notification(self.stdscr, ['Unexpected Error'])
|
||||
_logger.exception(e)
|
||||
else:
|
||||
with self.loader(delay=0, message='Posting'):
|
||||
time.sleep(2.0)
|
||||
self.refresh_content()
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import curses
|
||||
import time
|
||||
import logging
|
||||
|
||||
import requests
|
||||
import praw
|
||||
|
||||
@@ -14,6 +16,8 @@ from .curses_helpers import (BULLET, UARROW, DARROW, GOLD, Color,
|
||||
|
||||
__all__ = ['opened_links', 'SubredditController', 'SubredditPage']
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
# Used to keep track of browsing history across the current session
|
||||
opened_links = set()
|
||||
|
||||
@@ -62,7 +66,7 @@ class SubredditPage(BasePage):
|
||||
"Open a prompt to search the given subreddit"
|
||||
|
||||
name = name or self.content.name
|
||||
prompt = 'Search:'
|
||||
prompt = 'Search {}:'.format(name)
|
||||
query = self.prompt_input(prompt)
|
||||
if query is None:
|
||||
return
|
||||
@@ -110,7 +114,7 @@ class SubredditPage(BasePage):
|
||||
"Post a new submission to the given subreddit"
|
||||
|
||||
if not self.reddit.is_logged_in():
|
||||
show_notification(self.stdscr, ['Not logged in'])
|
||||
show_notification(self.stdscr, ['Login to post'])
|
||||
return
|
||||
|
||||
# Strips the subreddit to just the name
|
||||
@@ -129,7 +133,7 @@ class SubredditPage(BasePage):
|
||||
|
||||
# Validate the submission content
|
||||
if not submission_text:
|
||||
curses.flash()
|
||||
show_notification(self.stdscr, ['Post canceled'])
|
||||
return
|
||||
|
||||
if '\n' not in submission_text:
|
||||
@@ -139,9 +143,15 @@ class SubredditPage(BasePage):
|
||||
try:
|
||||
title, content = submission_text.split('\n', 1)
|
||||
self.reddit.submit(sub, title, text=content)
|
||||
except praw.errors.APIException:
|
||||
curses.flash()
|
||||
except praw.errors.APIException as e:
|
||||
message = ['Error: {}'.format(e.error_type), e.message]
|
||||
show_notification(self.stdscr, message)
|
||||
_logger.exception(e)
|
||||
except requests.HTTPError as e:
|
||||
show_notification(self.stdscr, ['Unexpected Error'])
|
||||
_logger.exception(e)
|
||||
else:
|
||||
with self.loader(delay=0, message='Posting'):
|
||||
time.sleep(2.0)
|
||||
self.refresh_content()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user