diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2019-06-04 20:41:59 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2019-06-04 20:43:55 +0200 |
commit | 9e36b3b77f8dffde071427d54ebc357ccf1de4af (patch) | |
tree | 7f2b43638132a082d9626ae3b084bd607141e519 | |
parent | 913e9c9e847ce753fcfb2354894682018d03084d (diff) | |
download | wee-slack-9e36b3b77f8dffde071427d54ebc357ccf1de4af.tar.gz |
Add error handling for /slack upload
-rw-r--r-- | wee_slack.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/wee_slack.py b/wee_slack.py index cf908dc..3419719 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -38,6 +38,11 @@ try: except ImportError: from urllib import urlencode +try: + from json import JSONDecodeError +except: + JSONDecodeError = ValueError + # hack to make tests possible.. better way? try: import weechat @@ -4087,6 +4092,13 @@ def command_upload(data, current_buffer, args): channel = EVENTROUTER.weechat_controller.buffers[current_buffer] file_path = os.path.expanduser(args) + if channel.type == 'team': + w.prnt('', "ERROR: Can't upload a file to the team buffer") + return w.WEECHAT_RC_ERROR + + if not os.path.isfile(file_path): + w.prnt('', 'ERROR: Could not find file: {}'.format(file_path)) + return w.WEECHAT_RC_ERROR post_data = { 'channels': channel.identifier, @@ -4106,13 +4118,30 @@ def command_upload(data, current_buffer, args): options.append(proxy_string) options_hashtable = {'arg{}'.format(i + 1): arg for i, arg in enumerate(options)} - w.hook_process_hashtable('curl', options_hashtable, config.slack_timeout, '', '') + w.hook_process_hashtable('curl', options_hashtable, config.slack_timeout, 'upload_callback', '') return w.WEECHAT_RC_OK_EAT command_upload.completion = '%(filename)' @utf8_decode +def upload_callback(data, command, return_code, out, err): + if return_code != 0: + w.prnt("", "ERROR: Couldn't upload file. Got return code {}. Error: {}".format(return_code, err)) + return w.WEECHAT_RC_OK_EAT + + try: + response = json.loads(out) + except JSONDecodeError: + w.prnt("", "ERROR: Couldn't process response from file upload. Got: {}".format(out)) + return w.WEECHAT_RC_OK_EAT + + if not response["ok"]: + w.prnt("", "ERROR: Couldn't upload file. Error: {}".format(response["error"])) + return w.WEECHAT_RC_OK_EAT + + +@utf8_decode def away_command_cb(data, current_buffer, args): all_servers, message = re.match('^/away( -all)? ?(.*)', args).groups() if all_servers: |