1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

Use raise from in modules (#11097)

* Use raise from.

* Add changelog fragment.

* Add comment.
This commit is contained in:
Felix Fontein 2025-11-12 21:00:17 +01:00 committed by GitHub
parent 2b4333a033
commit 40aea793ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 60 additions and 39 deletions

View file

@ -194,7 +194,7 @@ class _CmdRunnerContext:
except MissingArgumentValue:
raise
except Exception as e:
raise FormatError(arg_name, value, runner.arg_formats[arg_name], e)
raise FormatError(arg_name, value, runner.arg_formats[arg_name], e) from e
if self.check_mode_skip and module.check_mode:
return self.check_mode_return

View file

@ -45,7 +45,7 @@ def initialize_dialect(dialect, **kwargs):
try:
csv.register_dialect("custom", dialect, **dialect_params)
except TypeError as e:
raise CustomDialectFailureError(f"Unable to create custom dialect: {e}")
raise CustomDialectFailureError(f"Unable to create custom dialect: {e}") from e
dialect = "custom"
return dialect

View file

@ -56,8 +56,8 @@ def _find_end_quote(identifier, quote_char):
while True:
try:
quote = identifier.index(quote_char)
except ValueError:
raise UnclosedQuoteError
except ValueError as e:
raise UnclosedQuoteError from e
accumulate = accumulate + quote
try:
next_char = identifier[quote + 1]
@ -67,8 +67,8 @@ def _find_end_quote(identifier, quote_char):
try:
identifier = identifier[quote + 2 :]
accumulate = accumulate + 2
except IndexError:
raise UnclosedQuoteError
except IndexError as e:
raise UnclosedQuoteError from e
else:
return accumulate

View file

@ -59,14 +59,14 @@ def session_method_wrapper(f):
url = self.endpoint + url
r = f(self, url, *args, **kwargs)
except Exception as ex:
raise HwcClientException(0, f"Sending request failed, error={ex}")
raise HwcClientException(0, f"Sending request failed, error={ex}") from ex
result = None
if r.content:
try:
result = r.json()
except Exception as ex:
raise HwcClientException(0, f"Parsing response to json failed, error: {ex}")
raise HwcClientException(0, f"Parsing response to json failed, error: {ex}") from ex
code = r.status_code
if code not in [200, 201, 202, 203, 204, 205, 206, 207, 208, 226]:
@ -184,7 +184,7 @@ class Config:
try:
url = client.get_endpoint(service_type=service_type, region_name=region, interface="public")
except Exception as ex:
raise HwcClientException(0, f"Getting endpoint failed, error={ex}")
raise HwcClientException(0, f"Getting endpoint failed, error={ex}") from ex
if url == "":
raise HwcClientException(0, f"Cannot find the endpoint for {service_type}")

View file

@ -214,11 +214,11 @@ def _token_request(module_params, payload):
return r["access_token"]
except ValueError as e:
raise KeycloakError(f"API returned invalid JSON when trying to obtain access token from {auth_url}: {e}")
except KeyError:
raise KeycloakError(f"API did not include access_token field in response from {auth_url}")
raise KeycloakError(f"API returned invalid JSON when trying to obtain access token from {auth_url}: {e}") from e
except KeyError as e:
raise KeycloakError(f"API did not include access_token field in response from {auth_url}") from e
except Exception as e:
raise KeycloakError(f"Could not obtain access token from {auth_url}: {e}", authError=e)
raise KeycloakError(f"Could not obtain access token from {auth_url}: {e}", authError=e) from e
def _request_token_using_credentials(module_params):

View file

@ -37,7 +37,7 @@ def _env_then_dns_fallback(*args, **kwargs):
try:
return socket.gethostbyaddr(socket.gethostbyname("ipa-ca"))[0]
except Exception:
raise AnsibleFallbackNotFound
raise AnsibleFallbackNotFound from None # no need to pass the original exception's context since this is basically a special return value
class IPAClient:

View file

@ -116,7 +116,7 @@ class LXDClient:
self._raise_err_from_json(resp_json)
return resp_json
except socket.error as e:
raise LXDClientException("cannot connect to the LXD server", err=e)
raise LXDClientException("cannot connect to the LXD server", err=e) from e
def _raise_err_from_json(self, resp_json):
err_params = {}

View file

@ -322,4 +322,4 @@ def pritunl_auth_request(
validate_certs=validate_certs,
)
except Exception as e:
raise PritunlException(e)
raise PritunlException(e) from e