From f9cbb819fbbe4cca0d82b3ff0c20c34a02338fec Mon Sep 17 00:00:00 2001 From: richkamulda <48223448+richkamulda@users.noreply.github.com> Date: Thu, 26 Jan 2023 21:42:30 -1000 Subject: [PATCH] add exception handling when terraform is not on path; windows does not give helpful FileNotFoundError messages --- python_terraform/terraform.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/python_terraform/terraform.py b/python_terraform/terraform.py index 590dcec..043b136 100644 --- a/python_terraform/terraform.py +++ b/python_terraform/terraform.py @@ -337,9 +337,28 @@ def cmd( if self.is_env_vars_included: environ_vars = os.environ.copy() - p = subprocess.Popen( - cmds, stdout=stdout, stderr=stderr, cwd=working_folder, env=environ_vars - ) + try: + p = subprocess.Popen( + cmds, stdout=stdout, stderr=stderr, cwd=working_folder, env=environ_vars + ) + except FileNotFoundError as fnfe_exc: + # if no other path to the terraform binary was provided + if self.terraform_bin_path == "terraform": + msg = ( + "The 'terraform' command failed to invoke. Ensure the terraform " \ + "binary is on Path or provide the correct path to the binary." + ) + try: + # keep the command error to get to logs + raise TerraformCommandError( + 1, " ".join(cmds), out="", err=type(fnfe_exc).__name__ + ) from fnfe_exc + except TerraformCommandError as tfce_exc: + # add context for why 'terraform' invoke failed + raise RuntimeError(msg) from tfce_exc + + else: + raise TerraformCommandError(1, " ".join(cmds), out="", err=repr(fnfe_exc)) from fnfe_exc if not synchronous: return None, None, None