Mercurial > hg > AuthRPC
changeset 28:bea0c77734ca 0.3.0a
Changed/renamed exceptions
author | Ben Croston <ben@croston.org> |
---|---|
date | Tue, 28 Feb 2012 13:49:17 +0000 |
parents | 689d75bb60c2 |
children | b631d13b252d |
files | AuthRPC/client/__init__.py AuthRPC/tests.py CHANGELOG.txt README.txt setup.py |
diffstat | 5 files changed, 54 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/AuthRPC/client/__init__.py Tue Feb 28 13:18:33 2012 +0000 +++ b/AuthRPC/client/__init__.py Tue Feb 28 13:49:17 2012 +0000 @@ -44,7 +44,7 @@ request['method'] = self.name if len(args) > 0 and len(kwargs) > 0: - raise ProtocolError('Cannot use both positional and keyword arguments.') + raise JSONRPCProtocolError('Cannot use both positional and keyword arguments.') if len(args) > 0: params = copy.copy(args) @@ -64,12 +64,12 @@ resp = self.call(json.dumps(request)) if resp is None: - raise Exception('Server response is None') + raise JSONRPCProtocolError('Server response is None') resp = json.loads(resp.decode()) if resp['id'] != request['id']: - raise Exception('Inconsistent JSON request id returned') + raise JSONRPCProtocolError('Inconsistent JSON request id returned') if resp['error'] is not None: - raise Exception('JSONRPC Server Exception:\n%s'%resp['error']['error']) + raise RemoteException(resp['error']['error']) ## btc fixme - also pass exception type (e.g. IntegrityError) return resp['result'] @@ -107,44 +107,47 @@ connection.request('POST', self.request_path, body=request_body, headers=self.headers) return connection.getresponse() -class BadRequestException(Exception): +class BadRequestError(Exception): """HTTP 400 - Bad Request""" def __init__(self, msg=''): Exception.__init__(self,'HTTP 400 - Bad Request\n%s'%msg) -class UnauthorisedException(Exception): +class UnauthorisedError(Exception): """HTTP 401 - Unauthorised""" def __init__(self): Exception.__init__(self,'HTTP 401 - Unauthorised') -class ForbiddenException(Exception): +class ForbiddenError(Exception): """HTTP 403 - Forbidden""" def __init__(self): Exception.__init__(self,'HTTP 403 - Forbidden') -class NotFoundException(Exception): +class NotFoundError(Exception): """HTTP 404 - Not Found""" def __init__(self): Exception.__init__(self,'HTTP 404 - Not Found') -class NetworkSocketException(Exception): - def __init__(self): - Exception.__init__(self,'Network socket exception') - -class BadGatewayException(Exception): +class BadGatewayError(Exception): """HTTP 502 - Bad Gateway""" def __init__(self): Exception.__init__(self,'HTTP 502 - Bad Gateway') -class InternalServerException(Exception): +class InternalServerError(Exception): """HTTP 500 - Internal Server Error""" def __init__(self): Exception.__init__(self,'HTTP 500 - Internal Server Error') -class ProtocolError(Exception): +class NetworkSocketError(Exception): + pass + +class JSONRPCProtocolError(Exception): """Raised when the JSONRPC protocol has been broken""" pass +class RemoteException(Exception): + """Raised when there is an exception raised on the server""" + pass + class ServerProxy(object): """ A client class to communicate with a AuthRPC server @@ -167,21 +170,21 @@ try: response = self.__transport.request(request) except socket.error: - raise NetworkSocketException + raise NetworkSocketError if response.status == 200: return response.read() elif response.status == 400: - raise BadRequestException(response.read().decode()) + raise BadRequestError(response.read().decode()) elif response.status == 401: - raise UnauthorisedException + raise UnauthorisedError elif response.status == 403: - raise ForbiddenException + raise ForbiddenError elif response.status == 404: - raise NotFoundException + raise NotFoundError elif response.status == 500: - raise InternalServerException + raise InternalServerError elif response.status == 502: - raise BadGatewayException + raise BadGatewayError else: raise Exception('HTTP Status %s'%response.status) @@ -210,7 +213,7 @@ self.request['method'] = self.name self.request['id'] = str(uuid4()) if len(args) > 0 and len(kwargs) > 0: - raise ProtocolError('Cannot use both positional and keyword arguments.') + raise JSONRPCProtocolError('Cannot use both positional and keyword arguments.') if len(args) > 0: self.request['params'] = copy.copy(args) elif len(kwargs) > 0: @@ -261,7 +264,7 @@ result = [] for i,r in enumerate(response): if r['id'] != self._queue[i].id: - raise Exception('Inconsistent JSON request id returned') + raise JSONRPCProtocolError('Inconsistent JSON request id returned') result.append(r['result']) # clear the queue
--- a/AuthRPC/tests.py Tue Feb 28 13:18:33 2012 +0000 +++ b/AuthRPC/tests.py Tue Feb 28 13:49:17 2012 +0000 @@ -70,42 +70,42 @@ ##### client vvv ##### class AuthTest(unittest.TestCase): def runTest(self): - from client import ServerProxy, UnauthorisedException + from client import ServerProxy, UnauthorisedError self.client = ServerProxy('http://localhost:1337/', username='testuser', password='s3cr3t', user_agent='InternetExploiter') - with self.assertRaises(UnauthorisedException): + with self.assertRaises(UnauthorisedError): self.client.api.mymethod() self.client = ServerProxy('http://localhost:1337/', username='testuser', password='wrongpassword', user_agent='AuthRPC_unittest') - with self.assertRaises(UnauthorisedException): + with self.assertRaises(UnauthorisedError): self.client.api.mymethod() self.client = ServerProxy('http://localhost:1337/', username='wronguser', password='s3cr3t', user_agent='AuthRPC_unittest') - with self.assertRaises(UnauthorisedException): + with self.assertRaises(UnauthorisedError): self.client.api.mymethod() @unittest.skipIf(NO_INTERNET, 'http://www.wyre-it.co.uk/ not contactable') class NotFoundTest(unittest.TestCase): def runTest(self): - from client import ServerProxy, NotFoundException + from client import ServerProxy, NotFoundError self.client = ServerProxy('http://www.wyre-it.co.uk/this_should_generate_404.txt') - with self.assertRaises(NotFoundException): + with self.assertRaises(NotFoundError): self.client.api.mymethod() class NetworkSocketTest(unittest.TestCase): def runTest(self): - from client import ServerProxy, NetworkSocketException + from client import ServerProxy, NetworkSocketError self.client = ServerProxy('http://localhost:666/') - with self.assertRaises(NetworkSocketException): + with self.assertRaises(NetworkSocketError): self.client.api.mymethod() class AuthRPCTests(unittest.TestCase): @@ -122,13 +122,14 @@ class ExceptionTest(AuthRPCTests): def runTest(self): - with self.assertRaises(Exception): + from client import RemoteException + with self.assertRaises(RemoteException): self.client.api.raiseexception() class BadRequestTest(AuthRPCTests): def runTest(self): - from client import BadRequestException - with self.assertRaises(BadRequestException): + from client import BadRequestError + with self.assertRaises(BadRequestError): self.client.api.FunctionDoesNotExist() class EchoTest(AuthRPCTests): @@ -152,8 +153,8 @@ class ProtocolErrorTest(AuthRPCTests): def runTest(self): - from client import ProtocolError - with self.assertRaises(ProtocolError): + from client import JSONRPCProtocolError + with self.assertRaises(JSONRPCProtocolError): self.client.api.test(1, '2', three=3) class FileTest(AuthRPCTests): @@ -165,18 +166,18 @@ class NonExistentFileTest(AuthRPCTests): def runTest(self): - from client import NotFoundException - with self.assertRaises(NotFoundException): + from client import NotFoundError + with self.assertRaises(NotFoundError): self.client.__getfile__('nonexistant.file') class BadAuthFileTest(unittest.TestCase): def runTest(self): - from client import ServerProxy, UnauthorisedException + from client import ServerProxy, UnauthorisedError self.client = ServerProxy('http://localhost:1337/', username='testuser', password='s3cr3t', user_agent='InternetExploiter') - with self.assertRaises(UnauthorisedException): + with self.assertRaises(UnauthorisedError): self.client.__getfile__('LICENCE.txt') class BatchTest(AuthRPCTests): @@ -192,10 +193,10 @@ class BadBatchTest(AuthRPCTests): def runTest(self): - from client import BatchCall, BadRequestException + from client import BatchCall, BadRequestError batch = BatchCall(self.client) batch.api.FunctionDoesNotExist() - with self.assertRaises(BadRequestException): + with self.assertRaises(BadRequestError): batch() class SetFinishedFlag(unittest.TestCase):
--- a/CHANGELOG.txt Tue Feb 28 13:18:33 2012 +0000 +++ b/CHANGELOG.txt Tue Feb 28 13:49:17 2012 +0000 @@ -1,6 +1,10 @@ Change Log ========== +0.3.0a +------ +- Changed/renamed exceptions that are generated (client) + 0.2.0a ------ - Added __getfile__ mechanism
--- a/README.txt Tue Feb 28 13:18:33 2012 +0000 +++ b/README.txt Tue Feb 28 13:49:17 2012 +0000 @@ -28,6 +28,7 @@ :: from AuthRPC.client import ServerProxy, BatchCall + client = ServerProxy('http://localhost:1234/', username='myuser', password='secret',
--- a/setup.py Tue Feb 28 13:18:33 2012 +0000 +++ b/setup.py Tue Feb 28 13:49:17 2012 +0000 @@ -18,9 +18,9 @@ extra['use_2to3'] = True setup(name = 'AuthRPC', - version = '0.2.0a', + version = '0.3.0a', packages = find_packages(), - install_requires = 'WebOb>=1.2b2', + install_requires = 'WebOb>=1.2b3', author = 'Ben Croston', author_email = 'ben@croston.org', description = 'A JSONRPC-like client and server with additions to enable authenticated requests',