mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-26 21:33:12 +00:00
Use raise from in modules (#11097)
* Use raise from. * Add changelog fragment. * Add comment.
This commit is contained in:
@@ -161,8 +161,8 @@ class CronVar:
|
||||
except IOError:
|
||||
# cron file does not exist
|
||||
return
|
||||
except Exception:
|
||||
raise CronVarError("Unexpected error:", sys.exc_info()[0])
|
||||
except Exception as e:
|
||||
raise CronVarError("Unexpected error:", sys.exc_info()[0]) from e
|
||||
else:
|
||||
# using safely quoted shell for now, but this really should be two non-shell calls instead. FIXME
|
||||
(rc, out, err) = self.module.run_command(self._read_user_execute(), use_unsafe_shell=True)
|
||||
@@ -218,8 +218,8 @@ class CronVar:
|
||||
except OSError:
|
||||
# cron file does not exist
|
||||
return False
|
||||
except Exception:
|
||||
raise CronVarError("Unexpected error:", sys.exc_info()[0])
|
||||
except Exception as e:
|
||||
raise CronVarError("Unexpected error:", sys.exc_info()[0]) from e
|
||||
|
||||
def parse_for_var(self, line):
|
||||
lexer = shlex.shlex(line)
|
||||
|
||||
@@ -475,7 +475,7 @@ class JenkinsPlugin:
|
||||
self.module.fail_json(msg=msg_status, details=info["msg"])
|
||||
except Exception as e:
|
||||
if dont_fail:
|
||||
raise FailedInstallingWithPluginManager(e)
|
||||
raise FailedInstallingWithPluginManager(e) from e
|
||||
else:
|
||||
self.module.fail_json(msg=msg_exception, details=to_native(e))
|
||||
|
||||
|
||||
@@ -775,7 +775,7 @@ class JIRA(StateModuleHelper):
|
||||
try:
|
||||
content = base64.b64decode(content)
|
||||
except binascii.Error as e:
|
||||
raise Exception(f"Unable to base64 decode file content: {e}")
|
||||
raise Exception(f"Unable to base64 decode file content: {e}") from e
|
||||
|
||||
lines = [
|
||||
f"--{boundary}",
|
||||
|
||||
@@ -136,7 +136,7 @@ def download_url(module, url, dest):
|
||||
with open(dest, "w") as f:
|
||||
shutil.copyfileobj(response, f)
|
||||
except IOError as e:
|
||||
raise ModuleError(f"Failed to write: {e}")
|
||||
raise ModuleError(f"Failed to write: {e}") from e
|
||||
|
||||
|
||||
def install_overlay(module, name, list_url=None):
|
||||
|
||||
@@ -296,12 +296,12 @@ def ss_parse(raw):
|
||||
protocol, state, recv_q, send_q, local_addr_port, peer_addr_port = cells
|
||||
else:
|
||||
protocol, state, recv_q, send_q, local_addr_port, peer_addr_port, process = cells
|
||||
except ValueError:
|
||||
except ValueError as e:
|
||||
# unexpected stdout from ss
|
||||
raise EnvironmentError(
|
||||
'Expected `ss` table layout "Netid, State, Recv-Q, Send-Q, Local Address:Port, Peer Address:Port" and'
|
||||
f'optionally "Process", but got something else: {line}'
|
||||
)
|
||||
) from e
|
||||
|
||||
conns = regex_conns.search(local_addr_port)
|
||||
pids = regex_pid.findall(process)
|
||||
|
||||
@@ -210,8 +210,8 @@ class OSXDefaults:
|
||||
elif data_type == "date":
|
||||
try:
|
||||
return datetime.strptime(value.split("+")[0].strip(), "%Y-%m-%d %H:%M:%S")
|
||||
except ValueError:
|
||||
raise OSXDefaultsException(f"Invalid date value: {value!r}. Required format yyy-mm-dd hh:mm:ss.")
|
||||
except ValueError as e:
|
||||
raise OSXDefaultsException(f"Invalid date value: {value!r}. Required format yyy-mm-dd hh:mm:ss.") from e
|
||||
elif data_type in ["int", "integer"]:
|
||||
if not OSXDefaults.is_int(value):
|
||||
raise OSXDefaultsException(f"Invalid integer value: {value!r}")
|
||||
@@ -219,8 +219,8 @@ class OSXDefaults:
|
||||
elif data_type == "float":
|
||||
try:
|
||||
value = float(value)
|
||||
except ValueError:
|
||||
raise OSXDefaultsException(f"Invalid float value: {value!r}")
|
||||
except ValueError as e:
|
||||
raise OSXDefaultsException(f"Invalid float value: {value!r}") from e
|
||||
return value
|
||||
elif data_type == "array":
|
||||
if not isinstance(value, list):
|
||||
|
||||
@@ -212,8 +212,8 @@ def parse_subnet_cidr(cidr):
|
||||
addr, prefixlen = cidr.split("/")
|
||||
try:
|
||||
prefixlen = int(prefixlen)
|
||||
except ValueError:
|
||||
raise Exception(f"Wrong prefix length in CIDR expression {cidr}")
|
||||
except ValueError as e:
|
||||
raise Exception(f"Wrong prefix length in CIDR expression {cidr}") from e
|
||||
return addr, prefixlen
|
||||
|
||||
|
||||
|
||||
@@ -210,7 +210,7 @@ def act_on_sshkeys(target_state, module, packet_conn):
|
||||
changed = True
|
||||
except Exception as e:
|
||||
_msg = f"while trying to remove sshkey {k.label}, id {k.id} {target_state}, got error: {e}"
|
||||
raise Exception(_msg)
|
||||
raise Exception(_msg) from e
|
||||
|
||||
return {"changed": changed, "sshkeys": [serialize_sshkey(k) for k in matching_sshkeys]}
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ class PSAdapter(metaclass=abc.ABCMeta):
|
||||
try:
|
||||
regex = re.compile(pattern, flags)
|
||||
except re.error as e:
|
||||
raise PSAdapterError(f"'{pattern}' is not a valid regular expression: {e}")
|
||||
raise PSAdapterError(f"'{pattern}' is not a valid regular expression: {e}") from e
|
||||
|
||||
return [p.pid for p in self._process_iter(*self.PATTERN_ATTRS) if self._matches_regex(p, regex)]
|
||||
|
||||
|
||||
@@ -371,8 +371,8 @@ class RHEVConn:
|
||||
api = API(url=url, username=user, password=password, insecure=str(insecure_api))
|
||||
api.test()
|
||||
self.conn = api
|
||||
except Exception:
|
||||
raise Exception("Failed to connect to RHEV-M.")
|
||||
except Exception as e:
|
||||
raise Exception("Failed to connect to RHEV-M.") from e
|
||||
|
||||
def __del__(self):
|
||||
self.conn.disconnect()
|
||||
|
||||
@@ -242,8 +242,8 @@ def absent(schema_facts, cursor, schema, usage_roles, create_roles):
|
||||
)
|
||||
try:
|
||||
cursor.execute(f"drop schema {schema_facts[schema_key]['name']} restrict")
|
||||
except pyodbc.Error:
|
||||
raise CannotDropError("Dropping schema failed due to dependencies.")
|
||||
except pyodbc.Error as e:
|
||||
raise CannotDropError("Dropping schema failed due to dependencies.") from e
|
||||
del schema_facts[schema_key]
|
||||
return True
|
||||
else:
|
||||
|
||||
@@ -282,8 +282,8 @@ def absent(user_facts, cursor, user, roles):
|
||||
update_roles(user_facts, cursor, user, user_facts[user_key]["roles"], user_facts[user_key]["default_roles"], [])
|
||||
try:
|
||||
cursor.execute(f"drop user {user_facts[user_key]['name']}")
|
||||
except pyodbc.Error:
|
||||
raise CannotDropError("Dropping user failed due to dependencies.")
|
||||
except pyodbc.Error as e:
|
||||
raise CannotDropError("Dropping user failed due to dependencies.") from e
|
||||
del user_facts[user_key]
|
||||
return True
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user