Debian Codegraph @

test/integration/targets/module_tracebacks/library/ansibull.py

raw
1 from __future__ import annotations
2
3 from ansible.module_utils.basic import AnsibleModule
4
5
6 class Die(BaseException):
7 """Raise an exception that is unlikely to be caught (and will thus catastrophically exit the process)."""
8
9
10 def main():
11 module = AnsibleModule(
12 argument_spec=dict(
13 errors=dict(type='list', elements='str', default=[]),
14 trigger_unhandled_exception=dict(type='str', default=''),
15 return_from_main=dict(type='bool', default=False),
16 ),
17 supports_check_mode=True,
18 mutually_exclusive=[['errors', 'trigger_unhandled_exception', 'return_from_main']],
19 )
20
21 if module.params['return_from_main']:
22 return
23
24 if trigger_unhandled_exception := module.params['trigger_unhandled_exception']:
25 raise Die(trigger_unhandled_exception)
26
27 if errors := module.params['errors']:
28 raise_exceptions(errors)
29
30 result = dict()
31
32 module.exit_json(**result)
33
34
35 def raise_exceptions(exceptions: list[str]) -> None:
36 if len(exceptions) > 1:
37 try:
38 raise_exceptions(exceptions[1:])
39 except Exception as ex:
40 raise Exception(exceptions[0]) from ex
41
42 raise Exception(exceptions[0])
43
44
45 if __name__ == '__main__':
46 main()