Skip to content

Commit

Permalink
Refactor: Added exceptions
Browse files Browse the repository at this point in the history
* wip

* feat: added diagnostics structures

Diagnostic and DiagnosticsReport are used to construct
and communicate information about diagnostics state.

* Updated with new changes in hm-diag

* Exception Code Merge

* Bump Version for testing

* File changes

* Fix test

* Remove binaries

* Added more catches for exceptions

- Added Gateway_MFR File not found exception (Encountered when does not exist)
- Added Miner Failed To Fetch Mac Address exception
- Added Unit Test for Miner Failed To Fetch Mac Address exception

* Fix lint errors

* Flake linting fix

* Fix remaining lint errors

* Whitespace lines removed

* Fix PR warnings

* Version bump

* PR comment fixes

* Fix for linting

Co-authored-by: Marvin Arnold <marvin@geeky.rocks>
Co-authored-by: Sebastian <sebastian@ghostistudios.com>
  • Loading branch information
3 people authored Nov 12, 2021
1 parent c390352 commit a98ff5e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.11.4
current_version = 0.11.5
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion hm_pyhelper/diagnostics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from hm_pyhelper.diagnostics.diagnostics_report import DiagnosticsReport # noqa F401
from hm_pyhelper.diagnostics.diagnostic import Diagnostic # noqa F401
from hm_pyhelper.diagnostics.diagnostic import Diagnostic # noqa F401
8 changes: 8 additions & 0 deletions hm_pyhelper/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ class SPIUnavailableException(Exception):

class ECCMalfunctionException(Exception):
pass


class GatewayMFRFileNotFoundException(Exception):
pass


class MinerFailedToFetchMacAddress(Exception):
pass
9 changes: 8 additions & 1 deletion hm_pyhelper/miner_json_rpc/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class MinerJSONRPCException(Exception):
pass

Expand All @@ -13,3 +12,11 @@ class MinerMalformedURL(MinerJSONRPCException):

class MinerRegionUnset(MinerJSONRPCException):
pass


class MinerFailedFetchData(MinerJSONRPCException):
pass


class MinerFailedToFetchEthernetAddress(MinerJSONRPCException):
pass
35 changes: 31 additions & 4 deletions hm_pyhelper/miner_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from hm_pyhelper.lock_singleton import lock_ecc
from hm_pyhelper.logger import get_logger
from hm_pyhelper.exceptions import MalformedRegionException, \
SPIUnavailableException, ECCMalfunctionException
SPIUnavailableException, ECCMalfunctionException, \
GatewayMFRFileNotFoundException, \
MinerFailedToFetchMacAddress
from hm_pyhelper.miner_json_rpc.exceptions import \
MinerFailedToFetchEthernetAddress
from hm_pyhelper.hardware_definitions import is_rockpi


Expand Down Expand Up @@ -41,6 +45,11 @@ def run_gateway_mfr(args):
err_str = "gateway_mfr exited with a non-zero status"
LOGGER.exception(err_str)
raise ECCMalfunctionException(err_str).with_traceback(e.__traceback__)
except (FileNotFoundError, NotADirectoryError) as e:
err_str = "file/directory for gateway_mfr was not found"
LOGGER.exception(err_str)
raise GatewayMFRFileNotFoundException(err_str) \
.with_traceback(e.__traceback__)

try:
return json.loads(run_gateway_mfr_result.stdout)
Expand Down Expand Up @@ -170,6 +179,9 @@ def get_ethernet_addresses(diagnostics):
except Exception as e:
diagnostics[key] = False
LOGGER.error(e)
raise(MinerFailedToFetchEthernetAddress(
"Unable to fetch miner ethernet address.\
Exception: %s" % str(e))).with_traceback(e.__traceback__)


def get_mac_address(path):
Expand All @@ -182,13 +194,28 @@ def get_mac_address(path):
TypeError - If the function argument is not a string.
"""
if type(path) is not str:
raise TypeError("The path must be a string value")
raise TypeError(
"Constructing miner mac address failed.\
The path must be a string value")
try:
file = open(path)
except FileNotFoundError as e:
raise e
LOGGER.exception("Failed to find Miner \
Mac Address file at path %s" % path)
raise MinerFailedToFetchMacAddress(
"Failed to find file containing miner mac address. \
Exception: %s" % str(e)).with_traceback(e.__traceback__)
except PermissionError as e:
raise e
LOGGER.exception("Permissions invalid for Miner \
Mac Address file at path %s" % path)
raise MinerFailedToFetchMacAddress(
"Failed to fetch miner mac address. Invalid permissions to access file. \
Exception: %s" % str(e)).with_traceback(e.__traceback__)
except Exception as e:
LOGGER.exception(e)
raise MinerFailedToFetchMacAddress(
"Failed to fetch miner mac address. \
Exception: %s" % str(e)).with_traceback(e.__traceback__)
return file.readline().strip().upper()


Expand Down
3 changes: 1 addition & 2 deletions hm_pyhelper/tests/test_diagnostic_report.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import unittest
import json

from hm_pyhelper.diagnostics import Diagnostic
from hm_pyhelper.diagnostics import DiagnosticsReport
from hm_pyhelper.diagnostics import Diagnostic, DiagnosticsReport
from hm_pyhelper.diagnostics.diagnostics_report import \
DIAGNOSTICS_PASSED_KEY, \
DIAGNOSTICS_ERRORS_KEY
Expand Down
9 changes: 8 additions & 1 deletion hm_pyhelper/tests/test_miner_param.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from hm_pyhelper.exceptions import MinerFailedToFetchMacAddress
from hm_pyhelper.miner_param import retry_get_region, await_spi_available, \
provision_key, did_gateway_mfr_test_result_include_miner_key_pass
provision_key, did_gateway_mfr_test_result_include_miner_key_pass, \
get_mac_address
import unittest
from unittest.mock import mock_open, patch
import pytest

ALL_PASS_GATEWAY_MFR_TESTS = [
{
Expand Down Expand Up @@ -170,3 +173,7 @@ def test_get_region_from_miner(self, _):
@patch("os.path.exists", return_value=True)
def test_is_spi_available(self, _):
self.assertTrue(await_spi_available("spiXY.Z"))

def test_error_mac_address(self):
with pytest.raises(MinerFailedToFetchMacAddress):
get_mac_address("test/path")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='hm_pyhelper',
version='0.11.4',
version='0.11.5',
author="Nebra Ltd",
author_email="support@nebra.com",
description="Helium Python Helper",
Expand Down

0 comments on commit a98ff5e

Please sign in to comment.