From c19a892dd04e095b1635b453ae36899a3f4e1066 Mon Sep 17 00:00:00 2001 From: Willy Barro Date: Wed, 13 May 2015 20:45:50 -0300 Subject: [PATCH] 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. --- .../modules/extras/notification/pushbullet.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/ansible/modules/extras/notification/pushbullet.py b/lib/ansible/modules/extras/notification/pushbullet.py index 5b255b4b549..5e758507279 100644 --- a/lib/ansible/modules/extras/notification/pushbullet.py +++ b/lib/ansible/modules/extras/notification/pushbullet.py @@ -95,6 +95,7 @@ EXAMPLES = ''' try: from pushbullet import PushBullet + from pushbullet.errors import InvalidKeyError, PushError except ImportError: pushbullet_found = False else: @@ -131,8 +132,11 @@ def main(): module.fail_json(msg="Python 'pushbullet.py' module is required. Install via: $ pip install pushbullet.py") # Init pushbullet - pb = PushBullet(api_key) - target = None + try: + pb = PushBullet(api_key) + target = None + except InvalidKeyError: + module.fail_json(msg="Invalid api_key") # Checks for channel/device if device is None and channel is None: @@ -161,13 +165,13 @@ def main(): module.exit_json(changed=False, msg="OK") # Send push notification - success, result = target.push_note(title, body) - - if success: + try: + target.push_note(title, body) 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="Some error ocurred, Pushbullet response: %s" % (result)) + module.fail_json(msg="An unknown error has occurred") # import module snippets from ansible.module_utils.basic import *