Handle invalid api key and general api errors on pushbullet

pushbullet.py module has changed it's API and return types
so we're now handling these exceptions.
This commit is contained in:
Willy Barro 2015-05-13 20:45:50 -03:00
parent 318983ee53
commit a4232d657d

View file

@ -95,6 +95,7 @@ EXAMPLES = '''
try: try:
from pushbullet import PushBullet from pushbullet import PushBullet
from pushbullet.errors import InvalidKeyError, PushError
except ImportError: except ImportError:
pushbullet_found = False pushbullet_found = False
else: else:
@ -131,8 +132,11 @@ def main():
module.fail_json(msg="Python 'pushbullet.py' module is required. Install via: $ pip install pushbullet.py") module.fail_json(msg="Python 'pushbullet.py' module is required. Install via: $ pip install pushbullet.py")
# Init pushbullet # Init pushbullet
pb = PushBullet(api_key) try:
target = None pb = PushBullet(api_key)
target = None
except InvalidKeyError:
module.fail_json(msg="Invalid api_key")
# Checks for channel/device # Checks for channel/device
if device is None and channel is None: if device is None and channel is None:
@ -161,13 +165,13 @@ def main():
module.exit_json(changed=False, msg="OK") module.exit_json(changed=False, msg="OK")
# Send push notification # Send push notification
success, result = target.push_note(title, body) try:
target.push_note(title, body)
if success:
module.exit_json(changed=False, msg="OK") module.exit_json(changed=False, msg="OK")
except PushError as e:
module.fail_json(msg="An error occurred, Pushbullet's response: %s" % str(e))
# General failure module.fail_json(msg="An unknown error has occurred")
module.fail_json(msg="Some error ocurred, Pushbullet response: %s" % (result))
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *