User friendly messages for exceptions 69/33269/4
authorTaseer <taseer94@gmail.com>
Tue, 11 Apr 2017 16:39:18 +0000 (21:39 +0500)
committerTaseer <taseer94@gmail.com>
Thu, 13 Apr 2017 06:55:43 +0000 (11:55 +0500)
- Use colorama for color
- Implement verbosity option in separate patch

Change-Id: Ib2491d867e9bbf59cb00874d99a11f86ad7eea1b
Signed-off-by: Taseer Ahmed <taseer94@gmail.com>
qtip/base/error.py
qtip/cli/commands/cmd_metric.py
qtip/cli/commands/cmd_plan.py
qtip/cli/commands/cmd_qpi.py
requirements.txt
tests/unit/cli/cmd_metric_test.py
tests/unit/cli/cmd_plan_test.py
tests/unit/cli/cmd_qpi_test.py

index f23d8cd..d4b516a 100644 (file)
@@ -16,12 +16,14 @@ class InvalidContentError(BaseError):
     def __init__(self, filename, excinfo=None):
         self.filename = filename
         self.excinfo = excinfo
+        self.message = "Invalid content in {0}".format(filename)
 
 
 class NotFoundError(BaseError):
     def __init__(self, needle, heystack='qtip'):
         self.needle = needle
         self.heystack = heystack
+        self.message = "{0} not found in {1}".format(needle[0:-5], heystack)
 
 
 class ToBeDoneError(BaseError):
index a220844..1741fb4 100644 (file)
@@ -8,8 +8,11 @@
 ##############################################################################
 
 import click
+from colorama import Fore
 import os
 
+from qtip.base.error import InvalidContentError
+from qtip.base.error import NotFoundError
 from qtip.cli import utils
 from qtip.cli.entry import Context
 from qtip.loader.metric import MetricSpec
@@ -36,10 +39,16 @@ def cmd_list(ctx):
 @click.argument('name')
 @pass_context
 def show(ctx, name):
-    metric = MetricSpec('{}.yaml'.format(name))
-    cnt = metric.content
-    output = utils.render('metric', cnt)
-    click.echo(output)
+    try:
+        metric = MetricSpec('{}.yaml'.format(name))
+    except NotFoundError as nf:
+        click.echo(Fore.RED + "ERROR: metric spec: " + nf.message)
+    except InvalidContentError as ice:
+        click.echo(Fore.RED + "ERROR: metric spec " + ice.message)
+    else:
+        cnt = metric.content
+        output = utils.render('metric', cnt)
+        click.echo(output)
 
 
 @cli.command('run', help='Run performance test')
index beb61b0..b7c540b 100644 (file)
@@ -9,8 +9,11 @@
 
 
 import click
+from colorama import Fore
 import os
 
+from qtip.base.error import InvalidContentError
+from qtip.base.error import NotFoundError
 from qtip.cli import utils
 from qtip.cli.entry import Context
 from qtip.loader.plan import Plan
@@ -44,10 +47,16 @@ def list(ctx):
 @click.argument('name')
 @pass_context
 def show(ctx, name):
-    plan = Plan('{}.yaml'.format(name))
-    cnt = plan.content
-    output = utils.render('plan', cnt)
-    click.echo(output)
+    try:
+        plan = Plan('{}.yaml'.format(name))
+    except NotFoundError as nf:
+        click.echo(Fore.RED + "ERROR: plan spec: " + nf.message)
+    except InvalidContentError as ice:
+        click.echo(Fore.RED + "ERROR: plan spec: " + ice.message)
+    else:
+        cnt = plan.content
+        output = utils.render('plan', cnt)
+        click.echo(output)
 
 
 @cli.command('run', help='Execute a Plan')
index 1e3671c..a47442b 100644 (file)
@@ -9,8 +9,11 @@
 
 
 import click
+from colorama import Fore
 import os
 
+from qtip.base.error import InvalidContentError
+from qtip.base.error import NotFoundError
 from qtip.cli import utils
 from qtip.cli.entry import Context
 from qtip.loader.qpi import QPISpec
@@ -37,10 +40,16 @@ def cmd_list(ctx):
 @click.argument('name')
 @pass_context
 def show(ctx, name):
-    qpi = QPISpec('{}.yaml'.format(name))
-    cnt = qpi.content
-    output = utils.render('qpi', cnt)
-    click.echo(output)
+    try:
+        qpi = QPISpec('{}.yaml'.format(name))
+    except NotFoundError as nf:
+        click.echo(Fore.RED + "ERROR: qpi spec: " + nf.message)
+    except InvalidContentError as ice:
+        click.echo(Fore.RED + "ERROR: qpi spec: " + ice.message)
+    else:
+        cnt = qpi.content
+        output = utils.render('qpi', cnt)
+        click.echo(output)
 
 
 @cli.command('run', help='Run performance tests for the specified QPI')
index b0926b5..0f40852 100644 (file)
@@ -1,3 +1,4 @@
+colorama
 ansible
 click
 connexion
index cd496ad..c92e944 100644 (file)
@@ -41,3 +41,6 @@ def test_show(runner):
 
     result = runner.invoke(cli, ['metric', 'show'])
     assert 'Missing argument "name".' in result.output
+
+    result = runner.invoke(cli, ['metric', 'show', 'xyz'])
+    assert "ERROR: metric spec: xyz not found" in result.output
index 30025ae..53a0480 100644 (file)
@@ -38,3 +38,6 @@ def test_show(runner):
 
     result = runner.invoke(cli, ['plan', 'show'])
     assert 'Missing argument "name".' in result.output
+
+    result = runner.invoke(cli, ['plan', 'show', 'xyz'])
+    assert "ERROR: plan spec: xyz not found" in result.output
index 3d2c261..e7823c9 100644 (file)
@@ -38,3 +38,6 @@ def test_show(runner):
 
     result = runner.invoke(cli, ['qpi', 'show'])
     assert 'Missing argument "name".' in result.output
+
+    result = runner.invoke(cli, ['qpi', 'show', 'xyz'])
+    assert "ERROR: qpi spec: xyz not found" in result.output