From c9c84784c9af09b91222f238181093b1806b9404 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Fri, 24 Mar 2017 17:18:17 -0400 Subject: [PATCH] restore http error codes, add some color --- CHANGELOG.md | 1 + lbrynet/lbrynet_daemon/DaemonCLI.py | 48 ++++++++++++++++++--------- lbrynet/lbrynet_daemon/auth/client.py | 2 +- lbrynet/lbrynet_daemon/auth/server.py | 9 +++-- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5add43f62..4448e3178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ at anytime. ### Added * Add `wallet_list` command * Add checks for missing/extraneous params when calling jsonrpc commands + * Added colors to cli error messages * ### Changed diff --git a/lbrynet/lbrynet_daemon/DaemonCLI.py b/lbrynet/lbrynet_daemon/DaemonCLI.py index d0063e54b..a242c55cd 100644 --- a/lbrynet/lbrynet_daemon/DaemonCLI.py +++ b/lbrynet/lbrynet_daemon/DaemonCLI.py @@ -2,6 +2,7 @@ import argparse import json import os import sys +import colorama from lbrynet import conf from lbrynet.core import utils @@ -13,6 +14,7 @@ from httplib import UNAUTHORIZED def main(): + colorama.init() parser = argparse.ArgumentParser(add_help=False) _, arguments = parser.parse_known_args() @@ -64,7 +66,7 @@ def main(): os.path.basename(sys.argv[0])) ) else: - print_help_response(api.call('help', params)) + print_help_for_command(api, params['command']) elif method not in api.commands(): print_error("'" + method + "' is not a valid command.") @@ -77,23 +79,30 @@ def main(): print result else: print utils.json_dumps_pretty(result) - except (RPCError, KeyError, JSONRPCException) as err: - # TODO: The api should return proper error codes - # and messages so that they can be passed along to the user - # instead of this generic message. - # https://app.asana.com/0/158602294500137/200173944358192 - print "Something went wrong, here's the usage for %s:" % method - print_help_response(api.call('help', {'command': method})) - if hasattr(err, 'msg'): + except (RPCError, KeyError, JSONRPCException, HTTPError) as err: + error_data = None + if isinstance(err, HTTPError): + error_body = err.read() + try: + error_data = json.loads(error_body) + except ValueError: + print ( + "There was an error, and the response was not valid JSON.\n" + + "Raw JSONRPC response:\n" + error_body + ) + return 1 + + print_error(error_data['error']['message'] + "\n", suggest_help=False) + else: + print_error("Something went wrong\n", suggest_help=False) + + print_help_for_command(api, method) + if 'data' in error_data['error'] and 'traceback' in error_data['error']['data']: print "Here's the traceback for the error you encountered:" - print err.msg + print "\n".join(error_data['error']['data']['traceback']) return 1 -def print_help_response(help_response): - print help_response['help'] if 'help' in help_response else help_response - - def parse_params(params): if len(params) > 1: return get_params_from_kwargs(params) @@ -144,7 +153,8 @@ def print_help_suggestion(): def print_error(message, suggest_help=True): - print "ERROR: " + message + error_style = colorama.Style.BRIGHT + colorama.Fore.RED + print error_style + "ERROR: " + message + colorama.Style.RESET_ALL if suggest_help: print_help_suggestion() @@ -168,6 +178,14 @@ def print_help(api): ]) +def print_help_for_command(api, command): + help_response = api.call('help', {'command': command}) + print "Help for %s method:" % command + message = help_response['help'] if 'help' in help_response else help_response + message = "\n".join([' ' + line for line in message.split("\n")]) + print message + + def wrap_list_to_term_width(l, width=None, separator=', ', prefix=''): if width is None: try: diff --git a/lbrynet/lbrynet_daemon/auth/client.py b/lbrynet/lbrynet_daemon/auth/client.py index 455edd5ce..019fc7c50 100644 --- a/lbrynet/lbrynet_daemon/auth/client.py +++ b/lbrynet/lbrynet_daemon/auth/client.py @@ -44,7 +44,7 @@ class AuthAPIClient(object): def call(self, method, params={}): self.__id_count += 1 pre_auth_post_data = { - 'version': '1.1', + 'version': '2', 'method': method, 'params': params, 'id': self.__id_count diff --git a/lbrynet/lbrynet_daemon/auth/server.py b/lbrynet/lbrynet_daemon/auth/server.py index 9cdd8d4db..267791da1 100644 --- a/lbrynet/lbrynet_daemon/auth/server.py +++ b/lbrynet/lbrynet_daemon/auth/server.py @@ -197,11 +197,10 @@ class AuthJSONRPCServer(AuthorizedBase): response_content = jsonrpc_dumps_pretty(error, id=id_) self._set_headers(request, response_content) - # TODO: uncomment this after fixing lbrynet-cli to handle error code responses correctly - # try: - # request.setResponseCode(JSONRPCError.HTTP_CODES[error.code]) - # except KeyError: - # request.setResponseCode(JSONRPCError.HTTP_CODES[JSONRPCError.CODE_INTERNAL_ERROR]) + try: + request.setResponseCode(JSONRPCError.HTTP_CODES[error.code]) + except KeyError: + request.setResponseCode(JSONRPCError.HTTP_CODES[JSONRPCError.CODE_INTERNAL_ERROR]) self._render_message(request, response_content) @staticmethod