Ensure CoreExceptions are handled gracefully (#476)

Ensure CoreExceptions are handled gracefully

SUMMARY

CoreExceptions, when raised, should have a reasonably helpful and
actionable message associated with them. This adds a final check in
module execution to gracefully fail from these exceptions. A new
fail_from_exception method is added both to simplify exiting the module,
and to ensure that any chained exceptions are available when using -vvv.

ISSUE TYPE

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Joseph Torcasso <None>
This commit is contained in:
Mike Graves
2022-06-15 09:26:24 -04:00
committed by GitHub
parent 92785f58da
commit beb53652db
14 changed files with 104 additions and 26 deletions

View File

@@ -1,3 +1,5 @@
import traceback
from typing import Optional
from ansible_collections.kubernetes.core.plugins.module_utils.version import (
@@ -72,6 +74,13 @@ class AnsibleK8SModule:
def fail_json(self, *args, **kwargs):
return self._module.fail_json(*args, **kwargs)
def fail_from_exception(self, exception):
msg = to_text(exception)
tb = "".join(
traceback.format_exception(None, exception, exception.__traceback__)
)
return self.fail_json(msg=msg, exception=tb)
def has_at_least(
self, dependency: str, minimum: Optional[str] = None, warn: bool = False
) -> bool: