1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-30 07:28:52 +00:00

[PR #11095/2b4333a0 backport][stable-12] Use raise from in plugins (#11129)

Use raise from in plugins (#11095)

* Use raise from.

* Add changelog fragment.

(cherry picked from commit 2b4333a033)

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
patchback[bot] 2025-11-12 21:00:39 +01:00 committed by GitHub
parent cddb570e0e
commit cc93dab0fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 218 additions and 165 deletions

View file

@ -51,7 +51,7 @@ def counter(sequence):
except TypeError as e:
raise AnsibleFilterError(
f"community.general.counter needs a sequence with hashable elements (int, float or str) - {e}"
)
) from e
return result

View file

@ -98,7 +98,7 @@ def from_csv(data, dialect="excel", fieldnames=None, delimiter=None, skipinitial
try:
dialect = initialize_dialect(dialect, **dialect_params)
except (CustomDialectFailureError, DialectNotAvailableError) as e:
raise AnsibleFilterError(str(e))
raise AnsibleFilterError(str(e)) from e
reader = read_csv(data, dialect, fieldnames)
@ -108,7 +108,7 @@ def from_csv(data, dialect="excel", fieldnames=None, delimiter=None, skipinitial
for row in reader:
data_list.append(row)
except CSVError as e:
raise AnsibleFilterError(f"Unable to process file: {e}")
raise AnsibleFilterError(f"Unable to process file: {e}") from e
return data_list

View file

@ -79,7 +79,7 @@ def from_ini(obj):
try:
parser.read_file(StringIO(obj))
except Exception as ex:
raise AnsibleFilterError(f"from_ini failed to parse given string: {ex}", orig_exc=ex)
raise AnsibleFilterError(f"from_ini failed to parse given string: {ex}", orig_exc=ex) from ex
return parser.as_dict()

View file

@ -37,7 +37,7 @@ def initialize_hashids(**kwargs):
raise AnsibleFilterError(
"The provided parameters %s are invalid: %s"
% (", ".join(["%s=%s" % (k, v) for k, v in params.items()]), to_native(e))
)
) from e
def hashids_encode(nums, salt=None, alphabet=None, min_length=None):
@ -60,7 +60,7 @@ def hashids_encode(nums, salt=None, alphabet=None, min_length=None):
try:
hashid = hashids.encode(*nums)
except TypeError as e:
raise AnsibleTypeError(f"Data to encode must by a tuple or list of ints: {e}")
raise AnsibleTypeError(f"Data to encode must by a tuple or list of ints: {e}") from e
return hashid

View file

@ -149,7 +149,7 @@ def jc_filter(data, parser, quiet=True, raw=False):
return jc_parser.parse(data, quiet=quiet, raw=raw)
except Exception as e:
raise AnsibleFilterError(f"Error in jc filter plugin: {e}")
raise AnsibleFilterError(f"Error in jc filter plugin: {e}") from e
class FilterModule:

View file

@ -141,10 +141,10 @@ def json_query(data, expr):
try:
return jmespath.search(expr, data)
except jmespath.exceptions.JMESPathError as e:
raise AnsibleFilterError(f"JMESPathError in json_query filter plugin:\n{e}")
raise AnsibleFilterError(f"JMESPathError in json_query filter plugin:\n{e}") from e
except Exception as e:
# For older jmespath, we can get ValueError and TypeError without much info.
raise AnsibleFilterError(f"Error in jmespath.search in json_query filter plugin:\n{e}")
raise AnsibleFilterError(f"Error in jmespath.search in json_query filter plugin:\n{e}") from e
class FilterModule:

View file

@ -72,7 +72,7 @@ def to_ini(obj):
try:
ini_parser.read_dict(obj)
except Exception as ex:
raise AnsibleFilterError(f"to_ini failed to parse given dict:{ex}", orig_exc=ex)
raise AnsibleFilterError(f"to_ini failed to parse given dict:{ex}", orig_exc=ex) from ex
# catching empty dicts
if obj == dict():