Skip to content
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

refactor(exceptions): reduce redundancy, simplify message generation #1218

Merged
merged 2 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 0 additions & 3 deletions flopy/mf6/coordinates/modelgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ class MFGridException(Exception):
Model grid related exception
"""

def __init__(self, error):
Exception.__init__(self, f"MFGridException: {error}")


class ModelCell:
"""
Expand Down
48 changes: 13 additions & 35 deletions flopy/mf6/mfbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ class MFInvalidTransientBlockHeaderException(Exception):
Exception occurs when parsing a transient block header
"""

def __init__(self, error):
Exception.__init__(
self, f"MFInvalidTransientBlockHeaderException: {error}"
)


class ReadAsArraysException(Exception):
"""
Exception occurs when loading ReadAsArrays package as non-ReadAsArrays
package.
"""

def __init__(self, error):
Exception.__init__(self, f"ReadAsArraysException: {error}")


# external exceptions for users
class FlopyException(Exception):
Expand All @@ -37,7 +29,7 @@ class FlopyException(Exception):

def __init__(self, error, location=""):
self.message = error
Exception.__init__(self, f"FlopyException: {error} ({location})")
super().__init__(f"{error} ({location})")


class StructException(Exception):
Expand All @@ -47,7 +39,7 @@ class StructException(Exception):

def __init__(self, error, location):
self.message = error
Exception.__init__(self, f"StructException: {error} ({location})")
super().__init__(f"{error} ({location})")


class MFDataException(Exception):
Expand Down Expand Up @@ -132,35 +124,21 @@ def __init__(
self.org_type, self.org_value, self.org_traceback
)
# build error string
error_message_0 = "An error occurred in "
error_message = "An error occurred in "
if self.data_element is not None and self.data_element != "":
error_message_1 = f'data element "{self.data_element}" '
else:
error_message_1 = ""
error_message += f'data element "{self.data_element}" '
if self.model is not None and self.model != "":
error_message_2 = f'model "{self.model}" '
else:
error_message_2 = ""
error_message_3 = f'package "{self.package}".'
error_message_4 = (
' The error occurred while {} in the "{}" method'
".".format(self.current_process, self.method_caught_in)
error_message += f'model "{self.model}" '
error_message += (
f'package "{self.package}". The error occurred while '
f'{self.current_process} in the "{self.method_caught_in}" method.'
)
if len(self.messages) > 0:
error_message_5 = "\nAdditional Information:\n"
for index, message in enumerate(self.messages):
error_message_5 = f"{error_message_5}({index + 1}) {message}\n"
else:
error_message_5 = ""
error_message = "{}{}{}{}{}{}".format(
error_message_0,
error_message_1,
error_message_2,
error_message_3,
error_message_4,
error_message_5,
)
Exception.__init__(self, error_message)
error_message += "\nAdditional Information:\n"
error_message += "\n".join(
f"({idx}) {msg}" for (idx, msg) in enumerate(self.messages, 1)
)
super().__init__(error_message)


class VerbosityLevel(Enum):
Expand Down
45 changes: 20 additions & 25 deletions flopy/plot/plotutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
}


class PlotException(Exception):
def __init__(self, message):
super().__init__(message)


class PlotUtilities:
"""
Class which groups a collection of plotting utilities
Expand Down Expand Up @@ -662,13 +657,10 @@ def _plot_mflist_helper(

try:
arr = arr_dict[key]
except:
err_msg = "Cannot find key to plot\n"
err_msg += f" Provided key={key}\n Available keys="
for name, arr in arr_dict.items():
err_msg += f"{name}, "
err_msg += "\n"
raise PlotException(err_msg)
except KeyError:
err_msg = f'Cannot find key "{key}" to plot\n Available keys='
err_msg += ", ".join(str(k) for k in arr_dict.keys())
raise KeyError(err_msg)

axes = PlotUtilities._plot_array_helper(
arr,
Expand Down Expand Up @@ -1119,7 +1111,7 @@ def _plot_array_helper(

# check that matplotlib is installed
if plt is None:
raise PlotException(
raise ImportError(
"Could not import matplotlib. Must install matplotlib "
"in order to plot LayerFile data."
)
Expand Down Expand Up @@ -1270,7 +1262,7 @@ def _plot_bc_helper(
from .map import PlotMapView

if plt is None:
raise PlotException(
raise ImportError(
"Could not import matplotlib. Must install matplotlib "
"in order to plot boundary condition data."
)
Expand Down Expand Up @@ -2031,8 +2023,10 @@ def shapefile_extents(shp):

"""
if shapefile is None:
s = "Could not import shapefile. Must install pyshp in order to plot shapefiles."
raise PlotException(s)
raise ImportError(
"Could not import shapefile. "
"Must install pyshp in order to plot shapefiles."
)

sf = shapefile.Reader(shp)
shapes = sf.shapes()
Expand Down Expand Up @@ -2073,8 +2067,10 @@ def shapefile_get_vertices(shp):

"""
if shapefile is None:
s = "Could not import shapefile. Must install pyshp in order to plot shapefiles."
raise PlotException(s)
raise ImportError(
"Could not import shapefile. "
"Must install pyshp in order to plot shapefiles."
)

sf = shapefile.Reader(shp)
shapes = sf.shapes()
Expand Down Expand Up @@ -2124,9 +2120,9 @@ def shapefile_to_patch_collection(shp, radius=500.0, idx=None):

"""
if shapefile is None:
raise PlotException(
"Could not import shapefile. Must install pyshp "
"in order to plot shapefiles."
raise ImportError(
"Could not import shapefile. "
"Must install pyshp in order to plot shapefiles."
)
if plt is None:
raise ImportError(
Expand Down Expand Up @@ -2272,11 +2268,10 @@ def plot_shapefile(
"""

if shapefile is None:
s = (
"Could not import shapefile. Must install pyshp in "
"order to plot shapefiles."
raise ImportError(
"Could not import shapefile. "
"Must install pyshp in order to plot shapefiles."
)
raise PlotException(s)

vmin = kwargs.pop("vmin", None)
vmax = kwargs.pop("vmax", None)
Expand Down