-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement | Catch and handle HCL parsing errors #296
Changes from all commits
9f1740b
0f56d03
221624a
d1ebccf
5410137
4575b64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Binbash Leverage Command-line tool. | ||
""" | ||
|
||
# pylint: disable=wrong-import-position | ||
|
||
__version__ = "0.0.0" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Definitions for internal use of the cli. | ||
""" | ||
|
||
from functools import wraps | ||
|
||
import click | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Env variables loading utility. | ||
""" | ||
|
||
from pathlib import Path | ||
|
||
from yaenv.core import Env | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Binbash Leverage Command-line tool. | ||
""" | ||
|
||
import click | ||
|
||
from leverage import __version__ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Logging utilities. | ||
""" | ||
|
||
import logging | ||
from functools import wraps | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,13 +2,12 @@ | |||||||||||||||||||
from pathlib import Path | ||||||||||||||||||||
from configparser import NoSectionError, NoOptionError | ||||||||||||||||||||
|
||||||||||||||||||||
import hcl2 | ||||||||||||||||||||
import boto3 | ||||||||||||||||||||
from configupdater import ConfigUpdater | ||||||||||||||||||||
from botocore.exceptions import ClientError | ||||||||||||||||||||
from configupdater import ConfigUpdater | ||||||||||||||||||||
|
||||||||||||||||||||
from leverage import logger | ||||||||||||||||||||
from leverage._utils import key_finder, ExitError, get_or_create_section | ||||||||||||||||||||
from leverage._utils import key_finder, ExitError, get_or_create_section, parse_tf_file | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
class SkipProfile(Exception): | ||||||||||||||||||||
|
@@ -66,8 +65,7 @@ def get_profiles(cli): | |||||||||||||||||||
# these are files from the layer we are currently on | ||||||||||||||||||||
for name in ("config.tf", "locals.tf"): | ||||||||||||||||||||
try: | ||||||||||||||||||||
with open(name) as tf_file: | ||||||||||||||||||||
tf_config = hcl2.load(tf_file) | ||||||||||||||||||||
tf_config = parse_tf_file(Path(name)) | ||||||||||||||||||||
except FileNotFoundError: | ||||||||||||||||||||
continue | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -76,8 +74,7 @@ def get_profiles(cli): | |||||||||||||||||||
raw_profiles.update(set(key_finder(tf_config, "profile", "lookup"))) | ||||||||||||||||||||
|
||||||||||||||||||||
# the profile value from <layer>/config/backend.tfvars | ||||||||||||||||||||
with open(cli.paths.local_backend_tfvars) as backend_config_file: | ||||||||||||||||||||
backend_config = hcl2.load(backend_config_file) | ||||||||||||||||||||
backend_config = parse_tf_file(cli.paths.local_backend_tfvars) | ||||||||||||||||||||
tf_profile = backend_config["profile"] | ||||||||||||||||||||
Comment on lines
+77
to
78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for backend configuration parsing. While the layer configuration files have error handling (try-except), the backend configuration parsing lacks similar protection. A malformed backend.tfvars could lead to unclear errors. Consider wrapping the backend configuration parsing in a try-except block: - backend_config = parse_tf_file(cli.paths.local_backend_tfvars)
- tf_profile = backend_config["profile"]
+ try:
+ backend_config = parse_tf_file(cli.paths.local_backend_tfvars)
+ tf_profile = backend_config["profile"]
+ except FileNotFoundError:
+ raise ExitError(40, f"Backend configuration file not found: {cli.paths.local_backend_tfvars}")
+ except KeyError:
+ raise ExitError(40, "Missing 'profile' in backend configuration") 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
return tf_profile, raw_profiles | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Credentials managing module. | ||
""" | ||
|
||
import csv | ||
import json | ||
import re | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Module for managing Leverage projects. | ||
""" | ||
|
||
import re | ||
from pathlib import Path | ||
from shutil import copy2 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Tasks running module. | ||
""" | ||
|
||
import re | ||
|
||
import click | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve error handling in parse_tf_file.
The error handling can be enhanced to:
raise ... from err
Apply this diff to improve error handling:
🧰 Tools
🪛 Ruff (0.8.2)
127-127: Within an
except
clause, raise exceptions withraise ... from err
orraise ... from None
to distinguish them from errors in exception handling(B904)