Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : master
---------------------------------------------------------------
commit 911def0d0ebe02ad64d28f440437c4f58c7f3fb7
Author: Adam Samalik <asamalik(a)redhat.com>
Date: Thu Apr 17 11:35:15 2014 +0200
[cli] shell return codes with errors
---------------------------------------------------------------
cli/copr_cli/copr_exceptions.py | 14 ++++++++++++++
cli/copr_cli/main.py | 7 +++++++
cli/copr_cli/subcommands.py | 28 ++++++++++++++--------------
3 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/cli/copr_cli/copr_exceptions.py b/cli/copr_cli/copr_exceptions.py
index bb9c009..4be2f06 100644
--- a/cli/copr_cli/copr_exceptions.py
+++ b/cli/copr_cli/copr_exceptions.py
@@ -23,3 +23,17 @@ class CoprCliConfigException(CoprCliException):
malformated.
"""
pass
+
+
+class CoprCliRequestException(Exception):
+ """ Exception thrown when server says no. For example,
+ the user provided wrong project name or build ID.
+ """
+ pass
+
+
+class CoprCliUnknownResponseException(Exception):
+ """ Exception thrown when the response is unknown to cli.
+ It usualy means that something is broken.
+ """
+ pass
diff --git a/cli/copr_cli/main.py b/cli/copr_cli/main.py
index a1dc532..50d8a06 100644
--- a/cli/copr_cli/main.py
+++ b/cli/copr_cli/main.py
@@ -144,6 +144,10 @@ def main(argv=sys.argv[1:]):
except KeyboardInterrupt:
sys.stderr.write("\nInterrupted by user.")
sys.exit(1)
+ except copr_exceptions.CoprCliRequestException, e:
+ sys.stderr.write("\nSomething went wrong:")
+ sys.stderr.write("\nError: {0}\n".format(e))
+ sys.exit(1)
except argparse.ArgumentTypeError, e:
sys.stderr.write("\nError: {0}".format(e))
sys.exit(2)
@@ -154,6 +158,9 @@ def main(argv=sys.argv[1:]):
sys.stderr.write("\nError: {0}\n".format(e))
sys.stderr.write("Lines in INI file should not be indented.\n")
sys.exit(4)
+ except copr_exceptions.CoprCliUnknownResponseException, e:
+ sys.stderr.write("\nError: {0}\n".format(e))
+ sys.exit(5)
# except Exception as e:
# print "Error: {0}".format(e)
# sys.exit(100)
diff --git a/cli/copr_cli/subcommands.py b/cli/copr_cli/subcommands.py
index 92c775b..2a74ea0 100644
--- a/cli/copr_cli/subcommands.py
+++ b/cli/copr_cli/subcommands.py
@@ -20,29 +20,29 @@ import copr_exceptions
def _get_data(req, user, copr=None):
""" Wrapper around response from server
- check data and print nice error in case of some error (and return None)
- otherwise return json object.
+ checks data and raises a CoprCliRequestException with nice error message
+ or CoprCliUnknownResponseException in case of some some error.
+ Otherwise return json object.
"""
if "<title>Sign in Coprs</title>" in req.text:
- sys.stderr.write("Invalid API token\n")
+ raise copr_exceptions.CoprCliRequestException("Invalid API token\n")
return
if req.status_code == 404:
if copr is None:
- sys.stderr.write("User {0} is
unknown.\n".format(user["username"]))
+ raise copr_exceptions.CoprCliRequestException(
+ "User {0} is
unknown.\n".format(user["username"]))
else:
- sys.stderr.write("Project {0}/{1} not found.\n".format(
- (user["username"], copr)))
- return
+ raise copr_exceptions.CoprCliRequestException(
+ "Project {0}/{1} not found.\n".format(
+ user["username"], copr))
try:
output = json.loads(req.text)
except ValueError:
- sys.stderr.write("Unknown response from server.\n")
- return
+ raise copr_exceptions.CoprCliUnknownResponseException(
+ "Unknown response from the server.")
if req.status_code != 200:
- sys.stderr.write(
- "Something went wrong:\n {0}\n".format(output["error"]))
- return
+ raise copr_exceptions.CoprCliRequestException(output["error"])
return output
@@ -131,8 +131,8 @@ def create(name, chroots=[], description=None, instructions=None,
repos=None, initial_pkgs=None):
""" Create a new copr. """
if chroots is None:
- sys.stderr.write("Error: At least one chroot must be selected\n")
- sys.exit(1)
+ raise copr_exceptions.CoprCliRequestException(
+ "At least one chroot must be selected")
user = get_user()
copr_api_url = get_api_url()