Skip to content

[UPGRADE] improved error handling by implementing error chaining (- W… #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions multicast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,51 +320,53 @@
try:
import sys
if sys.__name__ is None:
raise ImportError("FAIL: we could not import os. We're like in the matrix! ABORT.")
raise ModuleNotFoundError(
"FAIL: we could not import os. We're like in the matrix! ABORT."
) from None
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


try:
import argparse
if argparse.__name__ is None:
raise ImportError("FAIL: we could not import argparse. ABORT.")
raise ModuleNotFoundError("FAIL: we could not import argparse. ABORT.") from None
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


try:
import unicodedata
if unicodedata.__name__ is None:
raise ImportError("FAIL: we could not import unicodedata. ABORT.")
raise ModuleNotFoundError("FAIL: we could not import unicodedata. ABORT.") from None
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


try:
import socket
if socket.__name__ is None:
raise ImportError("FAIL: we could not import socket. ABORT.")
raise ModuleNotFoundError("FAIL: we could not import socket. ABORT.") from None
else: # pragma: no branch
socket.setdefaulttimeout(int(_MCAST_DEFAULT_TTL))
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


try:
import struct
if struct.__name__ is None:
raise ImportError("FAIL: we could not import struct. ABORT.")
raise ModuleNotFoundError("FAIL: we could not import struct. ABORT.") from None
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


try:
import abc
if abc.__name__ is None:
raise ImportError("FAIL: we could not import Abstract base class. ABORT.")
raise ModuleNotFoundError("FAIL: we could not import Abstract base class. ABORT.") from None
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


class mtool(abc.ABC):
Expand Down
6 changes: 3 additions & 3 deletions multicast/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
from . import sys as _sys
except Exception:
# Throw more relevant Error
raise ImportError(str("[CWE-440] Error Importing Python"))
raise ImportError(str("[CWE-440] Error Importing Python")) from None


try:
Expand Down Expand Up @@ -462,8 +462,8 @@ def doStep(self, *args, **kwargs):
This method selects either the `McastHEAR` or `McastRECV` class based on the daemon
dispatch flag and executes the corresponding step.

The RECV (via McastRECV) is the primitive sub-command to recieve a single multicas hunk.
The HEAR (via McastHEAR) is equivilant to running RECV in a loop to continually recive
The RECV (via McastRECV) is the primitive sub-command to receive a single multicas hunk.
The HEAR (via McastHEAR) is equivalent to running RECV in a loop to continually receive
multiple hunks. Most use-case will probably want to use HEAR instead of RECV.

Args:
Expand Down
10 changes: 5 additions & 5 deletions multicast/hear.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@
for unit in depends:
try:
if unit.__name__ is None: # pragma: no branch
raise ImportError(
raise ModuleNotFoundError(
str("[CWE-440] module failed to import {}.").format(str(unit))
)
) from None
except Exception: # pragma: no branch
raise ImportError(str("[CWE-758] Module failed completely."))
raise ModuleNotFoundError(str("[CWE-758] Module failed completely.")) from None
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


class McastServer(socketserver.UDPServer):
Expand Down Expand Up @@ -351,7 +351,7 @@ def handle(self):
)
)
if """STOP""" in str(data):
raise RuntimeError("SHUTDOWN")
raise RuntimeError("SHUTDOWN") from None


class McastHEAR(multicast.mtool):
Expand Down
8 changes: 4 additions & 4 deletions multicast/recv.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@
]
for unit in depends:
if unit.__name__ is None: # pragma: no branch
baton = ImportError(
baton = ModuleNotFoundError(
str("[CWE-440] module failed to import {}.").format(str(unit))
) # pragma: no cover
baton.module = unit # pragma: no cover
raise baton # pragma: no cover
raise baton from None # pragma: no cover
except Exception as err:
baton = ImportError(err, str("[CWE-758] Module failed completely."))
baton.module = __module__
baton.path = __file__
baton.__cause__ = err
raise baton
raise baton from err


def joinstep(groups, port, iface=None, bind_group=None, isock=None):
Expand Down Expand Up @@ -272,7 +272,7 @@ def joinstep(groups, port, iface=None, bind_group=None, isock=None):
)
sock.setsockopt(_socket.IPPROTO_IP, _socket.IP_ADD_MEMBERSHIP, mreq)
except Exception as err: # pragma: no branch
raise NotImplementedError("""[CWE-440] Not Implemented.""", err) # pragma: no cover
raise NotImplementedError("""[CWE-440] Not Implemented.""", err) from err # pragma: no cover
return sock


Expand Down
8 changes: 4 additions & 4 deletions multicast/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@
if unit.__name__ is None: # pragma: no branch
raise ImportError(
str("[CWE-440] module failed to import {}.").format(str(unit))
)
except Exception: # pragma: no branch
raise ImportError(str("[CWE-758] Module failed completely."))
) from None
except Exception as _cause: # pragma: no branch
raise ImportError(str("[CWE-758] Module failed completely.")) from _cause
except Exception as err:
raise ImportError(err)
raise ImportError(err) from err


class McastSAY(multicast.mtool):
Expand Down
2 changes: 1 addition & 1 deletion multicast/skt.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
baton.module = __module__
baton.path = __file__
baton.__cause__ = err
raise baton
raise baton from err


def genSocket():
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
from setuptools.config import read_configuration
except Exception:
from setuptools.config.setupcfg import read_configuration
except Exception:
raise NotImplementedError("""[CWE-440] Not Implemented.""")
except Exception as err:
raise NotImplementedError("""[CWE-440] Not Implemented.""") from err


def readFile(filename):
Expand Down Expand Up @@ -75,7 +75,7 @@ def readFile(filename):
if not any(aexpected_file in filename for aexpected_file in expected_files):
raise ValueError(str(
"""[CWE-706] Access to the file {} was not expected."""
).format(filename))
).format(filename)) from None
with open(str("""./{}""").format(str(filename))) as f:
theResult = f.read()
except Exception as err:
Expand Down
39 changes: 16 additions & 23 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,45 +44,45 @@
try:
import sys
if sys.__name__ is None: # pragma: no branch
raise ImportError("[CWE-440] OMG! we could not import sys. ABORT. ABORT.")
raise ModuleNotFoundError("[CWE-440] OMG! we could not import sys. ABORT. ABORT.") from None
except Exception as err: # pragma: no branch
raise ImportError(err)
raise ImportError(err) from err


try:
if 'os' not in sys.modules:
import os
else: # pragma: no branch
os = sys.modules["""os"""]
except Exception: # pragma: no branch
raise ImportError("[CWE-440] OS Failed to import.")
except Exception as err: # pragma: no branch
raise ModuleNotFoundError("[CWE-440] OS Failed to import.") from err


try:
if 'unittest' not in sys.modules:
import unittest
else: # pragma: no branch
unittest = sys.modules["""unittest"""]
except Exception: # pragma: no branch
raise ImportError("[CWE-440] unittest Failed to import.")
except Exception as err: # pragma: no branch
raise ModuleNotFoundError("[CWE-440] unittest Failed to import.") from err


try:
if 'functools' not in sys.modules:
import functools
else: # pragma: no branch
functools = sys.modules["""functools"""]
except Exception: # pragma: no branch
raise ImportError("[CWE-440] functools Failed to import.")
except Exception as err: # pragma: no branch
raise ModuleNotFoundError("[CWE-440] functools Failed to import.") from err


try:
if 'multicast' not in sys.modules:
import multicast # pylint: disable=cyclic-import - skipcq: PYL-R0401
else: # pragma: no branch
multicast = sys.modules["""multicast"""]
except Exception: # pragma: no branch
raise ImportError("[CWE-440] multicast Failed to import.")
except Exception as err: # pragma: no branch
raise ImportError("[CWE-440] multicast Failed to import.") from err


try:
Expand All @@ -109,16 +109,9 @@
if unit_test.__name__ is None: # pragma: no branch
raise ImportError(
str("Test module failed to import even the {} tests.").format(str(unit_test))
)
) from None
except Exception as impErr: # pragma: no branch
print(str(''))
print(str(type(impErr)))
print(str(impErr))
print(str((impErr.args)))
print(str(''))
impErr = None
del impErr # skipcq - cleanup any error leaks early
raise ImportError(str("[CWE-758] Test module failed completely."))
raise ImportError(str("[CWE-758] Test module failed completely.")) from impErr
except Exception as badErr: # pragma: no branch
print(str(''))
print(str(type(badErr)))
Expand All @@ -135,8 +128,8 @@
from tests import context
else: # pragma: no branch
context = sys.modules["""tests.context"""]
except Exception: # pragma: no branch
raise ImportError("[CWE-440] context Failed to import.")
except Exception as _cause: # pragma: no branch
raise ImportError("[CWE-440] context Failed to import.") from _cause


test_cases = (
Expand Down Expand Up @@ -168,8 +161,8 @@ def load_tests(loader, tests, pattern):
import doctest
else: # pragma: no branch
doctest = sys.modules["""doctest"""]
except Exception: # pragma: no branch
raise ImportError("[CWE-440] doctest Failed to import.")
except Exception as _cause: # pragma: no branch
raise ImportError("[CWE-440] doctest Failed to import.") from _cause
finder = doctest.DocTestFinder(verbose=True, recurse=True, exclude_empty=True)
suite = unittest.TestSuite()
for test_class in test_cases:
Expand Down
Loading
Loading