From 12ee94a429adf932009af5d25f5473efc03b9220 Mon Sep 17 00:00:00 2001 From: Robert Putt Date: Mon, 25 Oct 2021 21:19:05 +0100 Subject: [PATCH] Add customised exceptions --- hm_pyhelper/miner_json_rpc/client.py | 26 ++++++++++-- hm_pyhelper/miner_json_rpc/exceptions.py | 15 +++++++ hm_pyhelper/tests/test_miner_json_rpc.py | 52 ++++++++++++++++++++++++ setup.py | 2 +- 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 hm_pyhelper/miner_json_rpc/exceptions.py diff --git a/hm_pyhelper/miner_json_rpc/client.py b/hm_pyhelper/miner_json_rpc/client.py index 678007c..e9bc622 100644 --- a/hm_pyhelper/miner_json_rpc/client.py +++ b/hm_pyhelper/miner_json_rpc/client.py @@ -1,4 +1,8 @@ +import requests from jsonrpcclient import request +from hm_pyhelper.miner_json_rpc.exceptions import MinerConnectionError +from hm_pyhelper.miner_json_rpc.exceptions import MinerMalformedURL +from hm_pyhelper.miner_json_rpc.exceptions import MinerRegionUnset class Client(object): @@ -7,14 +11,30 @@ def __init__(self, url='http://helium-miner:4467'): self.url = url def __fetch_data(self, method, **kwargs): - response = request(self.url, method, **kwargs) - return response.data.result + try: + response = request(self.url, method, **kwargs) + return response.data.result + except requests.exceptions.ConnectionError: + raise MinerConnectionError( + "Unable to connect to miner %s" % self.url + ) + except requests.exceptions.MissingSchema: + raise MinerMalformedURL( + "Miner JSONRPC URL '%s' is not a valid URL" + % self.url + ) def get_height(self): return self.__fetch_data('info_height') def get_region(self): - return self.__fetch_data('info_region') + region = self.__fetch_data('info_region') + if not region.get('region'): + raise MinerRegionUnset( + "Miner at %s does not have an asserted region" + % self.url + ) + return region def get_summary(self): return self.__fetch_data('info_summary') diff --git a/hm_pyhelper/miner_json_rpc/exceptions.py b/hm_pyhelper/miner_json_rpc/exceptions.py new file mode 100644 index 0000000..2dbd78e --- /dev/null +++ b/hm_pyhelper/miner_json_rpc/exceptions.py @@ -0,0 +1,15 @@ + +class MinerJSONRPCException(Exception): + pass + + +class MinerConnectionError(MinerJSONRPCException): + pass + + +class MinerMalformedURL(MinerJSONRPCException): + pass + + +class MinerRegionUnset(MinerJSONRPCException): + pass diff --git a/hm_pyhelper/tests/test_miner_json_rpc.py b/hm_pyhelper/tests/test_miner_json_rpc.py index ade6d47..dc54ed7 100644 --- a/hm_pyhelper/tests/test_miner_json_rpc.py +++ b/hm_pyhelper/tests/test_miner_json_rpc.py @@ -1,6 +1,10 @@ import unittest import mock + from hm_pyhelper.miner_json_rpc import MinerClient +from hm_pyhelper.miner_json_rpc.exceptions import MinerRegionUnset +from hm_pyhelper.miner_json_rpc.exceptions import MinerMalformedURL +from hm_pyhelper.miner_json_rpc.exceptions import MinerConnectionError BASE_URL = 'http://helium-miner:4467' @@ -23,6 +27,34 @@ def test_instantiation(self): self.assertIsInstance(client, MinerClient) self.assertEqual(client.url, BASE_URL) + def test_malformed_url(self): + client = MinerClient(url='fakeurl') + + exception_raised = False + exception_type = None + try: + client.get_height() + except Exception as exc: + exception_raised = True + exception_type = exc + + self.assertTrue(exception_raised) + self.assertIsInstance(exception_type, MinerMalformedURL) + + def test_connection_error(self): + client = MinerClient(url='http://notarealminer:9999') + + exception_raised = False + exception_type = None + try: + client.get_height() + except Exception as exc: + exception_raised = True + exception_type = exc + + self.assertTrue(exception_raised) + self.assertIsInstance(exception_type, MinerConnectionError) + @mock.patch('hm_pyhelper.miner_json_rpc.client.request') def test_get_height(self, mock_json_rpc_client): mock_json_rpc_client.return_value = Response( @@ -38,6 +70,26 @@ def test_get_height(self, mock_json_rpc_client): ) self.assertEqual(result, {'epoch': 25612, 'height': 993640}) + @mock.patch('hm_pyhelper.miner_json_rpc.client.request') + def test_get_region_not_asserted(self, mock_json_rpc_client): + mock_json_rpc_client.return_value = Response( + data=Result( + result={'region': None} + ) + ) + client = MinerClient() + exception_raised = False + exception_type = None + + try: + client.get_region() + except Exception as exc: + exception_raised = True + exception_type = exc + + self.assertTrue(exception_raised) + self.assertIsInstance(exception_type, MinerRegionUnset) + @mock.patch('hm_pyhelper.miner_json_rpc.client.request') def test_get_region(self, mock_json_rpc_client): mock_json_rpc_client.return_value = Response( diff --git a/setup.py b/setup.py index 7c1245e..afbeb3c 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='hm_pyhelper', - version='0.8.9', + version='0.8.12', author="Nebra Ltd", author_email="support@nebra.com", description="Helium Python Helper",