Skip to content

Commit

Permalink
simplify smtk exceptions and moving detailed messages to web API
Browse files Browse the repository at this point in the history
  • Loading branch information
rizac committed Nov 24, 2024
1 parent 4a381c0 commit 4e5bfc7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions egsim/api/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def clean_gsim(self) -> dict[str, GMPE]:
if name not in ret:
ret[name] = gsim(name)
except ModelError as err:
self.add_error(key, str(err))
self.add_error(key, f'invalid model(s) {str(err)}')
return ret


Expand All @@ -308,7 +308,7 @@ def clean_imt(self) -> dict[str, IMT]:
try:
ret = harmonize_input_imts(value)
except ImtError as err:
self.add_error(key, str(err))
self.add_error(key, f'invalid intensity measure(s) {str(err)}')
return ret

def clean(self):
Expand Down
6 changes: 5 additions & 1 deletion egsim/api/forms/flatfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from egsim.smtk import (ground_motion_properties_required_by, FlatfileError,
intensity_measures_defined_for, get_sa_limits)
from egsim.smtk.flatfile import (read_flatfile, get_dtype_of, FlatfileMetadata,
query as flatfile_query, EVENT_ID_COLUMN_NAME)
query as flatfile_query, EVENT_ID_COLUMN_NAME,
IncompatibleColumnError)
from egsim.api import models
from egsim.api.forms import EgsimBaseForm, APIForm, GsimForm

Expand Down Expand Up @@ -116,6 +117,9 @@ def clean(self):
# (the former if file size > configurable threshold
# (https://stackoverflow.com/a/10758350):
dataframe = read_flatfile(u_flatfile)
except IncompatibleColumnError as ice:
self.add_error('flatfile', f'column names conflict {str(ice)}')
return cleaned_data
except FlatfileError as err:
self.add_error("flatfile", str(err))
return cleaned_data # no need to further process
Expand Down
3 changes: 3 additions & 0 deletions egsim/api/forms/residuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
from django.forms import BooleanField

from egsim.smtk.flatfile import MissingColumnError
from egsim.smtk.residuals import get_residuals, FlatfileError, Clabel
from egsim.smtk.ranking import get_measures_of_fit
from egsim.api.forms import APIForm
Expand Down Expand Up @@ -65,5 +66,7 @@ def output(self) -> pd.DataFrame:
if is_ranking:
return get_measures_of_fit(gsims, imts, residuals)
return residuals
except MissingColumnError as mce:
self.add_error('flatfile', f'missing column(s) {str(mce)}')
except FlatfileError as err:
self.add_error("flatfile", str(err))
11 changes: 4 additions & 7 deletions egsim/smtk/flatfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def cast_to_dtype(


class FlatfileError(InputError):
"""General flatfile column(s) error. Inherits from smtks.validators.InputError.
"""General flatfile column(s) error. Inherits from smtk.validators.InputError.
Note that the str representation equals the init arguments comma-separated:
FlatfileError(arg1, arg2, ...) -> f"{str(arg1)}, {str(arg2)}, ..."
See subclasses for details
Expand All @@ -464,18 +464,15 @@ class FlatfileError(InputError):
class MissingColumnError(FlatfileError, AttributeError, KeyError):
"""MissingColumnError. It inherits also from AttributeError and
KeyError to be compliant with pandas and OpenQuake"""

msg_prefix = 'missing column(s)'
pass


class IncompatibleColumnError(FlatfileError):

msg_prefix = 'column names conflict'
pass


class ColumnDataError(FlatfileError, ValueError, TypeError):

msg_prefix = 'invalid data for'
pass


# registered columns:
Expand Down
14 changes: 3 additions & 11 deletions egsim/smtk/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,17 @@ def validate_imt_sa_limits(model: GMPE, imts: dict[str, IMT]) -> dict[str, IMT]:
class InputError(ValueError):
"""Base **abstract** exception for any input error (model, imt, flatfile)"""

# The str representation of this Exception (and subclasses) is each arg in self.args
# (comma-separated), prefixed by `msg_prefix` (if given):
msg_prefix: str

def __str__(self):
"""Reformat ``str(self)``"""
args = ", ".join(sorted(str(a) for a in self.args))
try:
return f'{self.msg_prefix} {args}'
except AttributeError:
return args
return ", ".join(sorted(str(a) for a in self.args))


class ModelError(InputError):
msg_prefix = 'invalid model(s)'
pass


class ImtError(InputError):
msg_prefix = 'invalid intensity measure(s)'
pass


class IncompatibleModelImtError(InputError):
Expand Down

0 comments on commit 4e5bfc7

Please sign in to comment.