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