1
0
Fork 0
mirror of synced 2024-06-17 01:34:40 +12:00

Refactored error handling

This commit is contained in:
Ali Parlakci 2018-08-09 00:17:04 +03:00
parent 4c8de50880
commit c19d8ad71b
4 changed files with 16 additions and 38 deletions

View file

@ -385,10 +385,7 @@ def prepareAttributes():
GLOBAL.arguments.link = GLOBAL.arguments.link.strip("\"")
try:
ATTRIBUTES = LinkDesigner(GLOBAL.arguments.link)
except InvalidRedditLink:
raise InvalidRedditLink
ATTRIBUTES = LinkDesigner(GLOBAL.arguments.link)
if GLOBAL.arguments.search is not None:
ATTRIBUTES["search"] = GLOBAL.arguments.search
@ -418,7 +415,7 @@ def prepareAttributes():
ATTRIBUTES["submitted"] = True
if GLOBAL.arguments.sort == "rising":
raise InvalidSortingType
raise InvalidSortingType("Invalid sorting type has given")
ATTRIBUTES["limit"] = GLOBAL.arguments.limit
@ -665,27 +662,8 @@ def main():
try:
POSTS = getPosts(prepareAttributes())
except InsufficientPermission:
print("You do not have permission to do that")
sys.exit()
except NoMatchingSubmissionFound:
print("No matching submission was found")
sys.exit()
except NoRedditSupoort:
print("Reddit does not support that")
sys.exit()
except NoPrawSupport:
print("PRAW does not support that")
sys.exit()
except MultiredditNotFound:
print("Multireddit not found")
sys.exit()
except InvalidSortingType:
print("Invalid sorting type has given")
sys.exit()
except InvalidRedditLink:
print("Invalid reddit link")
sys.exit()
except Exception as exception:
print(f"{exception.__class__.__name__}: {exception}")
if POSTS is None:
print("I could not find any posts in that URL")

View file

@ -67,7 +67,7 @@ class NoMatchingSubmissionFound(Exception):
class NoPrawSupport(Exception):
pass
class NoRedditSupoort(Exception):
class NoRedditSupport(Exception):
pass
class MultiredditNotFound(Exception):

View file

@ -29,7 +29,7 @@ def LinkParser(LINK):
ShortLink = False
if not "reddit.com" in LINK:
raise InvalidRedditLink
raise InvalidRedditLink("Invalid reddit link")
SplittedLink = LINK.split("/")

View file

@ -9,7 +9,7 @@ from prawcore.exceptions import NotFound, ResponseException, Forbidden
from src.tools import GLOBAL, createLogFile, jsonFile, printToFile
from src.errors import (NoMatchingSubmissionFound, NoPrawSupport,
NoRedditSupoort, MultiredditNotFound,
NoRedditSupport, MultiredditNotFound,
InvalidSortingType, RedditLoginFailed,
InsufficientPermission)
@ -115,7 +115,7 @@ def getPosts(args):
reddit = beginPraw(config)
if args["sort"] == "best":
raise NoPrawSupport
raise NoPrawSupport("PRAW does not support that")
if "subreddit" in args:
if "search" in args:
@ -145,7 +145,7 @@ def getPosts(args):
if "search" in args:
if args["sort"] in ["hot","rising","controversial"]:
raise InvalidSortingType
raise InvalidSortingType("Invalid sorting type has given")
if "subreddit" in args:
print (
@ -169,16 +169,16 @@ def getPosts(args):
)
elif "multireddit" in args:
raise NoPrawSupport
raise NoPrawSupport("PRAW does not support that")
elif "user" in args:
raise NoPrawSupport
raise NoPrawSupport("PRAW does not support that")
elif "saved" in args:
raise NoRedditSupoort
raise ("Reddit does not support that")
if args["sort"] == "relevance":
raise InvalidSortingType
raise InvalidSortingType("Invalid sorting type has given")
if "saved" in args:
print(
@ -243,7 +243,7 @@ def getPosts(args):
) (**keyword_params)
)
except NotFound:
raise MultiredditNotFound
raise MultiredditNotFound("Multireddit not found")
elif "submitted" in args:
print (
@ -273,7 +273,7 @@ def getPosts(args):
reddit.redditor(args["user"]).upvoted(limit=args["limit"])
)
except Forbidden:
raise InsufficientPermission
raise InsufficientPermission("You do not have permission to do that")
elif "post" in args:
print("post: {post}\n".format(post=args["post"]).upper(),noPrint=True)
@ -385,7 +385,7 @@ def redditSearcher(posts,SINGLE_POST=False):
print()
return subList
else:
raise NoMatchingSubmissionFound
raise NoMatchingSubmissionFound("No matching submission was found")
def checkIfMatching(submission):
global gfycatCount