Skip to content

Commit

Permalink
Merge 8839ac5 into dcca53e
Browse files Browse the repository at this point in the history
  • Loading branch information
markmcclain committed Oct 26, 2020
2 parents dcca53e + 8839ac5 commit 91b9088
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion jsonrpclib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

# Easy access to utility methods and classes
from jsonrpclib.jsonrpc import Server, ServerProxy
from jsonrpclib.jsonrpc import MultiCall, Fault, ProtocolError, AppError
from jsonrpclib.jsonrpc import ( MultiCall, Fault,
ProtocolError, AppError, TransportError )
from jsonrpclib.jsonrpc import loads, dumps, load, dump
from jsonrpclib.jsonrpc import jloads, jdumps
import jsonrpclib.history as history
Expand Down
18 changes: 17 additions & 1 deletion jsonrpclib/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ def data(self):
return self.args[0][2]


class TransportError( ProtocolError ):
def __init__(self, url, errcode, errmsg, msg):
ProtocolError.__init__(self, url, errcode, errmsg, msg)

self.url = url
self.errcode = errcode
self.errmsg = errmsg
self.msg = msg

def __repr__(self):
return (
"<%s for %s: %s %s>" %
(self.__class__.__name__, self.url, self.errcode, self.errmsg)
)


class JSONParser(object):
"""
Default JSON parser
Expand Down Expand Up @@ -386,7 +402,7 @@ def single_request(self, host, handler, request_body, verbose=0):
# Discard any response data and raise exception
if response.getheader("content-length", 0):
response.read()
raise ProtocolError(host + handler,
raise TransportError(host + handler,
response.status, response.reason,
response.msg)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,14 @@ def test_multicall_failure(self):
def func():
return result[i]
self.assertRaises(raises[i], func)

def test_tranport_error(self):
"""
test http error handling
"""
badserver = jsonrpclib.ServerProxy(
"http://localhost:{0}/pathdoesnotexist".format(self.port),
history=self.history
)

self.assertRaises( jsonrpclib.TransportError, badserver.foo )

0 comments on commit 91b9088

Please sign in to comment.