Skip to content

Commit 6a84c0b

Browse files
markmcclaintcalmant
authored andcommitted
Add TransportError subclass of ProtocolError
The backwards compatible TransportError simplifies client error handling by providing dedicated subclass with attribute access for error details.
1 parent dcca53e commit 6a84c0b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

jsonrpclib/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
# Easy access to utility methods and classes
2929
from jsonrpclib.jsonrpc import Server, ServerProxy
30-
from jsonrpclib.jsonrpc import MultiCall, Fault, ProtocolError, AppError
30+
from jsonrpclib.jsonrpc import ( MultiCall, Fault,
31+
ProtocolError, AppError, TransportError )
3132
from jsonrpclib.jsonrpc import loads, dumps, load, dump
3233
from jsonrpclib.jsonrpc import jloads, jdumps
3334
import jsonrpclib.history as history

jsonrpclib/jsonrpc.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ def data(self):
202202
return self.args[0][2]
203203

204204

205+
class TransportError( ProtocolError ):
206+
def __init__(self, url, errcode, errmsg, msg):
207+
ProtocolError.__init__(self, url, errcode, errmsg, msg)
208+
209+
self.url = url
210+
self.errcode = errcode
211+
self.errmsg = errmsg
212+
self.msg = msg
213+
214+
def __repr__(self):
215+
return (
216+
"<%s for %s: %s %s>" %
217+
(self.__class__.__name__, self.url, self.errcode, self.errmsg)
218+
)
219+
220+
205221
class JSONParser(object):
206222
"""
207223
Default JSON parser
@@ -386,7 +402,7 @@ def single_request(self, host, handler, request_body, verbose=0):
386402
# Discard any response data and raise exception
387403
if response.getheader("content-length", 0):
388404
response.read()
389-
raise ProtocolError(host + handler,
405+
raise TransportError(host + handler,
390406
response.status, response.reason,
391407
response.msg)
392408

tests/test_internal.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,14 @@ def test_multicall_failure(self):
165165
def func():
166166
return result[i]
167167
self.assertRaises(raises[i], func)
168+
169+
def test_tranport_error(self):
170+
"""
171+
test http error handling
172+
"""
173+
badserver = jsonrpclib.ServerProxy(
174+
"http://localhost:{0}/pathdoesnotexist".format(self.port),
175+
history=self.history
176+
)
177+
178+
self.assertRaises( jsonrpclib.TransportError, badserver.foo )

0 commit comments

Comments
 (0)