Skip to content

Commit

Permalink
vdk-control-cli: improve execute error handling
Browse files Browse the repository at this point in the history
When an exception occurs while the `vdkcli execute`
command is used, we print the whole stack trace in the
terminal, which is not user-friendly, and could cause
confusion.

This change adds error handling to the execute command
that provides more manageable error message, and avoids
printing the whole stack trace of the exception. The
stacktrace itself is moved in a log debug message and
would be displayed in dev mode, or with the verbosity
option set to DEBUG

Testing Done: Existing unit tests and added a new
test

Signed-off-by: Andon Andonov <[email protected]>
  • Loading branch information
doks5 committed Aug 25, 2021
1 parent 6c8fe4c commit f949c9d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tabulate import tabulate
from taurus.vdk.control.configuration.defaults_config import load_default_team_name
from taurus.vdk.control.rest_lib.factory import ApiClientFactory
from taurus.vdk.control.rest_lib.rest_client_errors import ApiClientErrorDecorator
from taurus.vdk.control.utils import cli_utils
from taurus.vdk.control.utils.cli_utils import get_or_prompt
from taurus.vdk.control.utils.cli_utils import OutputFormat
Expand Down Expand Up @@ -51,6 +52,7 @@ def transform_execution(e: DataJobExecution):
elif output == OutputFormat.JSON.value:
return cli_utils.json_format(list(executions))

@ApiClientErrorDecorator()
def start(self, name: str, team: str, output: OutputFormat):
execution_request = DataJobExecutionRequest(
started_by=f"vdk-control-cli", args={}
Expand Down Expand Up @@ -79,12 +81,14 @@ def start(self, name: str, team: str, output: OutputFormat):
}
click.echo(json.dumps(result))

@ApiClientErrorDecorator()
def show(self, name: str, team: str, execution_id: str, output: OutputFormat):
execution: DataJobExecution = self.execution_api.data_job_execution_read(
team_name=team, job_name=name, execution_id=execution_id
)
click.echo(self.__model_executions([execution], output))

@ApiClientErrorDecorator()
def list(self, name: str, team: str, output: OutputFormat):
executions: list[DataJobExecution] = self.execution_api.data_job_execution_list(
team_name=team, job_name=name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# SPDX-License-Identifier: Apache-2.0
import functools
import json
import logging

from taurus.vdk.control.exception.vdk_exception import VDKException
from taurus_datajob_api import ApiException
from urllib3.exceptions import HTTPError

log = logging.getLogger(__name__)


class ApiClientErrorDecorator:
def __init__(self, what="Control Service Error"):
Expand Down Expand Up @@ -45,6 +48,7 @@ def decorated(*args, **kwargs):
result = fn(*args, **kwargs)
return result
except ApiException as ex:
log.debug(f"Exception Stacktrace: {ex}")
body = self._get_error(ex)
vdk_ex = VDKException(
what=body.get("what", self.what),
Expand All @@ -59,6 +63,7 @@ def decorated(*args, **kwargs):
)
raise vdk_ex from ex
except HTTPError as ex:
log.debug(f"Exception Stacktrace: {ex}")
vdk_ex = VDKException(
what=self.what,
why=str(ex),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ def test_execute_with_empty_url(httpserver: PluginHTTPServer, tmpdir: LocalPath)
result.exit_code == 2
), f"result exit code is not 2, result output: {result.output}, exc: {result.exc_info}"
assert "what" in result.output and "why" in result.output


def test_execute_with_exception(httpserver: PluginHTTPServer, tmpdir:LocalPath):
runner = CliRunner()
result = runner.invoke(execute, ["--start", "-n", "job_name", "-t", "team_name", "-u", "localhost"])

assert (
result.exit_code == 2
), f"result exit code is not 2, result output: {result.output}, exc: {result.exc_info}"
assert "what" in result.output and "why" in result.output

0 comments on commit f949c9d

Please sign in to comment.