1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

now has display of last resort

moved all display/color/err to use display.error
now also capture generic exceptions if they happen (never should!)
This commit is contained in:
Brian Coca 2015-07-05 17:46:51 -04:00
parent 6a75125f32
commit f42b6237d9

View file

@ -18,7 +18,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
######################################################## ########################################################
from __future__ import (absolute_import) from __future__ import (absolute_import, print_function)
__metaclass__ = type __metaclass__ = type
__requires__ = ['ansible'] __requires__ = ['ansible']
@ -38,10 +38,17 @@ import sys
from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError
from ansible.utils.display import Display from ansible.utils.display import Display
######################################################## ########################################
### OUTPUT OF LAST RESORT ###
class LastResort(object):
def error(self, msg):
print(msg, file=sys.stderr)
########################################
if __name__ == '__main__': if __name__ == '__main__':
display = LastResort()
cli = None cli = None
me = os.path.basename(sys.argv[0]) me = os.path.basename(sys.argv[0])
@ -70,21 +77,24 @@ if __name__ == '__main__':
except AnsibleOptionsError as e: except AnsibleOptionsError as e:
cli.parser.print_help() cli.parser.print_help()
display.display(str(e), stderr=True, color='red') display.error(str(e))
sys.exit(5) sys.exit(5)
except AnsibleParserError as e: except AnsibleParserError as e:
display.display(str(e), stderr=True, color='red') display.error(str(e))
sys.exit(4) sys.exit(4)
# TQM takes care of these, but leaving comment to reserve the exit codes # TQM takes care of these, but leaving comment to reserve the exit codes
# except AnsibleHostUnreachable as e: # except AnsibleHostUnreachable as e:
# display.display(str(e), stderr=True, color='red') # display.error(str(e))
# sys.exit(3) # sys.exit(3)
# except AnsibleHostFailed as e: # except AnsibleHostFailed as e:
# display.display(str(e), stderr=True, color='red') # display.error(str(e))
# sys.exit(2) # sys.exit(2)
except AnsibleError as e: except AnsibleError as e:
display.display(str(e), stderr=True, color='red') display.error(str(e))
sys.exit(1) sys.exit(1)
except KeyboardInterrupt: except KeyboardInterrupt:
display.error("interrupted") display.error("User interrupted execution")
sys.exit(99) sys.exit(99)
except Exception as e:
display.error("Unexpected Exception: %s" % str(e))
sys.exit(250)