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("\"")
|
GLOBAL.arguments.link = GLOBAL.arguments.link.strip("\"")
|
||||||
|
|
||||||
try:
|
ATTRIBUTES = LinkDesigner(GLOBAL.arguments.link)
|
||||||
ATTRIBUTES = LinkDesigner(GLOBAL.arguments.link)
|
|
||||||
except InvalidRedditLink:
|
|
||||||
raise InvalidRedditLink
|
|
||||||
|
|
||||||
if GLOBAL.arguments.search is not None:
|
if GLOBAL.arguments.search is not None:
|
||||||
ATTRIBUTES["search"] = GLOBAL.arguments.search
|
ATTRIBUTES["search"] = GLOBAL.arguments.search
|
||||||
|
@ -418,7 +415,7 @@ def prepareAttributes():
|
||||||
ATTRIBUTES["submitted"] = True
|
ATTRIBUTES["submitted"] = True
|
||||||
|
|
||||||
if GLOBAL.arguments.sort == "rising":
|
if GLOBAL.arguments.sort == "rising":
|
||||||
raise InvalidSortingType
|
raise InvalidSortingType("Invalid sorting type has given")
|
||||||
|
|
||||||
ATTRIBUTES["limit"] = GLOBAL.arguments.limit
|
ATTRIBUTES["limit"] = GLOBAL.arguments.limit
|
||||||
|
|
||||||
|
@ -665,27 +662,8 @@ def main():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
POSTS = getPosts(prepareAttributes())
|
POSTS = getPosts(prepareAttributes())
|
||||||
except InsufficientPermission:
|
except Exception as exception:
|
||||||
print("You do not have permission to do that")
|
print(f"{exception.__class__.__name__}: {exception}")
|
||||||
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()
|
|
||||||
|
|
||||||
if POSTS is None:
|
if POSTS is None:
|
||||||
print("I could not find any posts in that URL")
|
print("I could not find any posts in that URL")
|
||||||
|
|
|
@ -67,7 +67,7 @@ class NoMatchingSubmissionFound(Exception):
|
||||||
class NoPrawSupport(Exception):
|
class NoPrawSupport(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class NoRedditSupoort(Exception):
|
class NoRedditSupport(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class MultiredditNotFound(Exception):
|
class MultiredditNotFound(Exception):
|
||||||
|
|
|
@ -29,7 +29,7 @@ def LinkParser(LINK):
|
||||||
ShortLink = False
|
ShortLink = False
|
||||||
|
|
||||||
if not "reddit.com" in LINK:
|
if not "reddit.com" in LINK:
|
||||||
raise InvalidRedditLink
|
raise InvalidRedditLink("Invalid reddit link")
|
||||||
|
|
||||||
SplittedLink = LINK.split("/")
|
SplittedLink = LINK.split("/")
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ from prawcore.exceptions import NotFound, ResponseException, Forbidden
|
||||||
|
|
||||||
from src.tools import GLOBAL, createLogFile, jsonFile, printToFile
|
from src.tools import GLOBAL, createLogFile, jsonFile, printToFile
|
||||||
from src.errors import (NoMatchingSubmissionFound, NoPrawSupport,
|
from src.errors import (NoMatchingSubmissionFound, NoPrawSupport,
|
||||||
NoRedditSupoort, MultiredditNotFound,
|
NoRedditSupport, MultiredditNotFound,
|
||||||
InvalidSortingType, RedditLoginFailed,
|
InvalidSortingType, RedditLoginFailed,
|
||||||
InsufficientPermission)
|
InsufficientPermission)
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ def getPosts(args):
|
||||||
reddit = beginPraw(config)
|
reddit = beginPraw(config)
|
||||||
|
|
||||||
if args["sort"] == "best":
|
if args["sort"] == "best":
|
||||||
raise NoPrawSupport
|
raise NoPrawSupport("PRAW does not support that")
|
||||||
|
|
||||||
if "subreddit" in args:
|
if "subreddit" in args:
|
||||||
if "search" in args:
|
if "search" in args:
|
||||||
|
@ -145,7 +145,7 @@ def getPosts(args):
|
||||||
|
|
||||||
if "search" in args:
|
if "search" in args:
|
||||||
if args["sort"] in ["hot","rising","controversial"]:
|
if args["sort"] in ["hot","rising","controversial"]:
|
||||||
raise InvalidSortingType
|
raise InvalidSortingType("Invalid sorting type has given")
|
||||||
|
|
||||||
if "subreddit" in args:
|
if "subreddit" in args:
|
||||||
print (
|
print (
|
||||||
|
@ -169,16 +169,16 @@ def getPosts(args):
|
||||||
)
|
)
|
||||||
|
|
||||||
elif "multireddit" in args:
|
elif "multireddit" in args:
|
||||||
raise NoPrawSupport
|
raise NoPrawSupport("PRAW does not support that")
|
||||||
|
|
||||||
elif "user" in args:
|
elif "user" in args:
|
||||||
raise NoPrawSupport
|
raise NoPrawSupport("PRAW does not support that")
|
||||||
|
|
||||||
elif "saved" in args:
|
elif "saved" in args:
|
||||||
raise NoRedditSupoort
|
raise ("Reddit does not support that")
|
||||||
|
|
||||||
if args["sort"] == "relevance":
|
if args["sort"] == "relevance":
|
||||||
raise InvalidSortingType
|
raise InvalidSortingType("Invalid sorting type has given")
|
||||||
|
|
||||||
if "saved" in args:
|
if "saved" in args:
|
||||||
print(
|
print(
|
||||||
|
@ -243,7 +243,7 @@ def getPosts(args):
|
||||||
) (**keyword_params)
|
) (**keyword_params)
|
||||||
)
|
)
|
||||||
except NotFound:
|
except NotFound:
|
||||||
raise MultiredditNotFound
|
raise MultiredditNotFound("Multireddit not found")
|
||||||
|
|
||||||
elif "submitted" in args:
|
elif "submitted" in args:
|
||||||
print (
|
print (
|
||||||
|
@ -273,7 +273,7 @@ def getPosts(args):
|
||||||
reddit.redditor(args["user"]).upvoted(limit=args["limit"])
|
reddit.redditor(args["user"]).upvoted(limit=args["limit"])
|
||||||
)
|
)
|
||||||
except Forbidden:
|
except Forbidden:
|
||||||
raise InsufficientPermission
|
raise InsufficientPermission("You do not have permission to do that")
|
||||||
|
|
||||||
elif "post" in args:
|
elif "post" in args:
|
||||||
print("post: {post}\n".format(post=args["post"]).upper(),noPrint=True)
|
print("post: {post}\n".format(post=args["post"]).upper(),noPrint=True)
|
||||||
|
@ -385,7 +385,7 @@ def redditSearcher(posts,SINGLE_POST=False):
|
||||||
print()
|
print()
|
||||||
return subList
|
return subList
|
||||||
else:
|
else:
|
||||||
raise NoMatchingSubmissionFound
|
raise NoMatchingSubmissionFound("No matching submission was found")
|
||||||
|
|
||||||
def checkIfMatching(submission):
|
def checkIfMatching(submission):
|
||||||
global gfycatCount
|
global gfycatCount
|
||||||
|
|
Loading…
Reference in a new issue