forked from LBRYCommunity/lbry-sdk
Merge branch 'nicer_error_output'
* nicer_error_output: nicer error output
This commit is contained in:
commit
15ebc39f95
3 changed files with 30 additions and 16 deletions
|
@ -46,7 +46,7 @@ class AuthAPIClient(object):
|
||||||
pre_auth_post_data = {
|
pre_auth_post_data = {
|
||||||
'version': '1.1',
|
'version': '1.1',
|
||||||
'method': method,
|
'method': method,
|
||||||
'params': [params],
|
'params': params,
|
||||||
'id': self.__id_count
|
'id': self.__id_count
|
||||||
}
|
}
|
||||||
to_auth = get_auth_message(pre_auth_post_data)
|
to_auth = get_auth_message(pre_auth_post_data)
|
||||||
|
|
|
@ -12,7 +12,7 @@ from txjsonrpc import jsonrpclib
|
||||||
from lbrynet import conf
|
from lbrynet import conf
|
||||||
from lbrynet.core.Error import InvalidAuthenticationToken, InvalidHeaderError
|
from lbrynet.core.Error import InvalidAuthenticationToken, InvalidHeaderError
|
||||||
from lbrynet.core import utils
|
from lbrynet.core import utils
|
||||||
from lbrynet.lbrynet_daemon.auth.util import APIKey, get_auth_message
|
from lbrynet.lbrynet_daemon.auth.util import APIKey, get_auth_message, jsonrpc_dumps_pretty
|
||||||
from lbrynet.lbrynet_daemon.auth.client import LBRY_SECRET
|
from lbrynet.lbrynet_daemon.auth.client import LBRY_SECRET
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
@ -32,7 +32,10 @@ class JSONRPCException(Exception):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def faultString(self):
|
def faultString(self):
|
||||||
|
try:
|
||||||
return self.err.getTraceback()
|
return self.err.getTraceback()
|
||||||
|
except AttributeError:
|
||||||
|
return str(self.err)
|
||||||
|
|
||||||
|
|
||||||
class UnknownAPIMethodError(Exception):
|
class UnknownAPIMethodError(Exception):
|
||||||
|
@ -123,8 +126,13 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
|
|
||||||
def _render_error(self, failure, request, id_,
|
def _render_error(self, failure, request, id_,
|
||||||
version=jsonrpclib.VERSION_2, response_code=FAILURE):
|
version=jsonrpclib.VERSION_2, response_code=FAILURE):
|
||||||
err = JSONRPCException(Failure(failure), response_code)
|
# TODO: is it necessary to wrap the failure in a Failure? if not, merge this with next fn
|
||||||
fault = jsonrpclib.dumps(err, id=id_, version=version)
|
self._render_error_string(Failure(failure), request, id_, version, response_code)
|
||||||
|
|
||||||
|
def _render_error_string(self, error_string, request, id_, version=jsonrpclib.VERSION_2,
|
||||||
|
response_code=FAILURE):
|
||||||
|
err = JSONRPCException(error_string, response_code)
|
||||||
|
fault = jsonrpc_dumps_pretty(err, id=id_, version=version)
|
||||||
self._set_headers(request, fault)
|
self._set_headers(request, fault)
|
||||||
if response_code != AuthJSONRPCServer.FAILURE:
|
if response_code != AuthJSONRPCServer.FAILURE:
|
||||||
request.setResponseCode(response_code)
|
request.setResponseCode(response_code)
|
||||||
|
@ -163,13 +171,13 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
content = request.content.read()
|
content = request.content.read()
|
||||||
try:
|
try:
|
||||||
parsed = jsonrpclib.loads(content)
|
parsed = jsonrpclib.loads(content)
|
||||||
except ValueError as err:
|
except ValueError:
|
||||||
log.warning("Unable to decode request json")
|
log.warning("Unable to decode request json")
|
||||||
self._render_error(err, request, None)
|
self._render_error_string('Invalid JSON', request, None)
|
||||||
return server.NOT_DONE_YET
|
return server.NOT_DONE_YET
|
||||||
|
|
||||||
function_name = parsed.get('method')
|
function_name = parsed.get('method')
|
||||||
args = parsed.get('params')
|
args = parsed.get('params', {})
|
||||||
id_ = parsed.get('id')
|
id_ = parsed.get('id')
|
||||||
token = parsed.pop('hmac', None)
|
token = parsed.pop('hmac', None)
|
||||||
version = self._get_jsonrpc_version(parsed.get('jsonrpc'), id_)
|
version = self._get_jsonrpc_version(parsed.get('jsonrpc'), id_)
|
||||||
|
@ -298,14 +306,14 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
def _update_session_secret(self, session_id):
|
def _update_session_secret(self, session_id):
|
||||||
self.sessions.update({session_id: APIKey.new(name=session_id)})
|
self.sessions.update({session_id: APIKey.new(name=session_id)})
|
||||||
|
|
||||||
def _get_jsonrpc_version(self, version=None, id=None):
|
@staticmethod
|
||||||
|
def _get_jsonrpc_version(version=None, id_=None):
|
||||||
if version:
|
if version:
|
||||||
version_for_return = int(float(version))
|
return int(float(version))
|
||||||
elif id and not version:
|
elif id_:
|
||||||
version_for_return = jsonrpclib.VERSION_1
|
return jsonrpclib.VERSION_1
|
||||||
else:
|
else:
|
||||||
version_for_return = jsonrpclib.VERSION_PRE1
|
return jsonrpclib.VERSION_PRE1
|
||||||
return version_for_return
|
|
||||||
|
|
||||||
def _callback_render(self, result, request, id_, version, auth_required=False):
|
def _callback_render(self, result, request, id_, version, auth_required=False):
|
||||||
result_for_return = result
|
result_for_return = result
|
||||||
|
@ -315,7 +323,7 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
result_for_return = (result_for_return,)
|
result_for_return = (result_for_return,)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
encoded_message = jsonrpclib.dumps(
|
encoded_message = jsonrpc_dumps_pretty(
|
||||||
result_for_return, id=id_, version=version, default=default_decimal)
|
result_for_return, id=id_, version=version, default=default_decimal)
|
||||||
self._set_headers(request, encoded_message, auth_required)
|
self._set_headers(request, encoded_message, auth_required)
|
||||||
self._render_message(request, encoded_message)
|
self._render_message(request, encoded_message)
|
||||||
|
@ -323,5 +331,6 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
log.exception("Failed to render API response: %s", result)
|
log.exception("Failed to render API response: %s", result)
|
||||||
self._render_error(err, request, id_, version)
|
self._render_error(err, request, id_, version)
|
||||||
|
|
||||||
def _render_response(self, result):
|
@staticmethod
|
||||||
|
def _render_response(result):
|
||||||
return defer.succeed(result)
|
return defer.succeed(result)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import yaml
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
from txjsonrpc import jsonrpclib
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -23,6 +24,10 @@ def generate_key(x=None):
|
||||||
return sha(x)
|
return sha(x)
|
||||||
|
|
||||||
|
|
||||||
|
def jsonrpc_dumps_pretty(obj, **kwargs):
|
||||||
|
return jsonrpclib.dumps(obj, sort_keys=True, indent=2, separators=(',', ': '), **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class APIKey(object):
|
class APIKey(object):
|
||||||
def __init__(self, secret, name, expiration=None):
|
def __init__(self, secret, name, expiration=None):
|
||||||
self.secret = secret
|
self.secret = secret
|
||||||
|
|
Loading…
Reference in a new issue