Refactored error handling
This commit is contained in:
parent
4c8de50880
commit
c19d8ad71b
30
script.py
30
script.py
|
@ -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")
|
||||
|
|
|
@ -67,7 +67,7 @@ class NoMatchingSubmissionFound(Exception):
|
|||
class NoPrawSupport(Exception):
|
||||
pass
|
||||
|
||||
class NoRedditSupoort(Exception):
|
||||
class NoRedditSupport(Exception):
|
||||
pass
|
||||
|
||||
class MultiredditNotFound(Exception):
|
||||
|
|
|
@ -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("/")
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue