1616# You should have received a copy of the GNU Lesser General Public License '
1717# along with geoh5py. If not, see <https://www.gnu.org/licenses/>. '
1818# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
19-
19+ # pylint: disable=arguments-differ
2020
2121from __future__ import annotations
2222
@@ -36,55 +36,49 @@ class Geoh5FileClosedError(ABC, Exception):
3636
3737
3838class BaseValidationError (ABC , Exception ):
39- """Base class for custom exceptions."""
39+ """
40+ Base class for custom exceptions.
41+ """
42+
43+ def __init__ (self , * args , ** kwargs ):
44+ super ().__init__ (self .message (* args , ** kwargs ))
4045
4146 @classmethod
4247 @abstractmethod
43- def message (cls , name , value , validation ) :
44- """Builds custom error message."""
45- raise NotImplementedError ()
48+ def message (cls , * args , ** kwargs ) -> str :
49+ """
50+ Builds custom error message.
4651
52+ Each subclass of BaseValidationError should implement the message class method.
53+ """
4754
48- class JSONParameterValidationError (Exception ):
49- """Error on uuid validation."""
5055
51- def __init__ ( self , name : str , err : str ):
52- super (). __init__ ( JSONParameterValidationError . message ( name , err ))
56+ class JSONParameterValidationError ( BaseValidationError ):
57+ """Error on uuid validation."""
5358
5459 @classmethod
55- def message (cls , name , err ) :
60+ def message (cls , name : str , err : str ) -> str :
5661 return f"Malformed ui.json dictionary for parameter '{ name } '. { err } "
5762
5863
5964class OptionalValidationError (BaseValidationError ):
6065 """Error if None value provided to non-optional parameter."""
6166
62- def __init__ (
63- self ,
64- name : str ,
65- value : Any | None ,
66- validation : bool ,
67- ):
68- super ().__init__ (OptionalValidationError .message (name , value , validation ))
69-
7067 @classmethod
71- def message (cls , name , value , validation ) :
68+ def message (cls , name : str , * _ ) -> str :
7269 return f"Cannot set a None value to non-optional parameter: { name } ."
7370
7471
7572class AssociationValidationError (BaseValidationError ):
7673 """Error on association between child and parent entity validation."""
7774
78- def __init__ (
79- self ,
75+ @classmethod
76+ def message (
77+ cls ,
8078 name : str ,
8179 value : Entity | PropertyGroup | UUID ,
8280 validation : Entity | Workspace ,
83- ):
84- super ().__init__ (AssociationValidationError .message (name , value , validation ))
85-
86- @classmethod
87- def message (cls , name , value , validation ):
81+ ) -> str :
8882 return (
8983 f"Property '{ name } ' with value: '{ value } ' must be "
9084 f"a child entity of parent { validation } "
@@ -94,46 +88,38 @@ def message(cls, name, value, validation):
9488class PropertyGroupValidationError (BaseValidationError ):
9589 """Error on property group validation."""
9690
97- def __init__ (self , name : str , value : PropertyGroup , validation : list [str ]):
98- super ().__init__ (PropertyGroupValidationError .message (name , value , validation ))
99-
10091 @classmethod
101- def message (cls , name , value , validation ) :
92+ def message (cls , name : str , value : PropertyGroup , validation : list [ str ]) -> str :
10293 return (
10394 f"Property group for '{ name } ' must be of type '{ validation } '. "
10495 f"Provided '{ value .name } ' of type '{ value .property_group_type } '"
10596 )
10697
10798
10899class AtLeastOneValidationError (BaseValidationError ):
109- def __init__ (self , name : str , value : list [str ]):
110- super ().__init__ (AtLeastOneValidationError .message (name , value ))
100+ """Error on at least one validation."""
111101
112102 @classmethod
113- def message (cls , name , value , validation = None ) :
103+ def message (cls , name : str , value : list [ str ], * _ ) -> str :
114104 opts = "'" + "', '" .join (str (k ) for k in value ) + "'"
115105 return f"Must provide at least one { name } . Options are: { opts } "
116106
117107
118108class RequiredValidationError (BaseValidationError ):
119- def __init__ (self , name : str ):
120- super ().__init__ (RequiredValidationError .message (name ))
109+ """Error on required parameter validation."""
121110
122111 @classmethod
123- def message (cls , name , value = None , validation = None ) :
112+ def message (cls , name : str , * _ ) -> str :
124113 return f"Missing required parameter: '{ name } '."
125114
126115
127116class ShapeValidationError (BaseValidationError ):
128117 """Error on shape validation."""
129118
130- def __init__ (
131- self , name : str , value : tuple [int , ...], validation : tuple [int , ...] | str
132- ):
133- super ().__init__ (ShapeValidationError .message (name , value , validation ))
134-
135- @staticmethod
136- def message (name , value , validation ):
119+ @classmethod
120+ def message (
121+ cls , name : str , value : tuple [int , ...], validation : tuple [int , ...] | str
122+ ) -> str :
137123 return (
138124 f"Parameter '{ name } ' with shape { value } was provided. "
139125 f"Expected { validation } ."
@@ -143,11 +129,8 @@ def message(name, value, validation):
143129class TypeValidationError (BaseValidationError ):
144130 """Error on type validation."""
145131
146- def __init__ (self , name : str , value : str , validation : str | list [str ]):
147- super ().__init__ (TypeValidationError .message (name , value , validation ))
148-
149- @staticmethod
150- def message (name , value , validation ):
132+ @classmethod
133+ def message (cls , name : str , value : str , validation : str | list [str ]) -> str :
151134 return f"Type '{ value } ' provided for '{ name } ' is invalid." + iterable_message (
152135 validation
153136 )
@@ -156,38 +139,50 @@ def message(name, value, validation):
156139class UUIDValidationError (BaseValidationError ):
157140 """Error on uuid string validation."""
158141
159- def __init__ (self , name : str , value : str ):
160- super ().__init__ (UUIDValidationError .message (name , value ))
161-
162- @staticmethod
163- def message (name , value , validation = None ):
142+ @classmethod
143+ def message (cls , name : str , value : str , * _ ) -> str :
164144 return f"Parameter '{ name } ' with value '{ value } ' is not a valid uuid string."
165145
166146
167147class ValueValidationError (BaseValidationError ):
168148 """Error on value validation."""
169149
170- def __init__ (self , name : str , value : Any , validation : list [Any ]):
171- super ().__init__ (ValueValidationError .message (name , value , validation ))
172-
173- @staticmethod
174- def message (name , value , validation ):
150+ @classmethod
151+ def message (cls , name : str , value : Any , validation : list [Any ]) -> str :
175152 return f"Value '{ value } ' provided for '{ name } ' is invalid." + iterable_message (
176153 validation
177154 )
178155
179156
180- def iterable_message (valid : list [Any ] | None ) -> str :
181- """Append possibly iterable valid: "Must be (one of): {valid}."."""
157+ class MemoryValidationError (BaseValidationError ):
158+ """Error on memory validation."""
159+
160+ @classmethod
161+ def message (cls , name : str , value : Any , validation : float ) -> str :
162+ return (
163+ f"Parameter '{ name } ' with value '{ value } ' "
164+ f"exceeds memory limit of { validation / 1e6 } MB."
165+ )
166+
167+
168+ def iterable_message (valid : str | list [Any ] | None ) -> str :
169+ """
170+ Append possibly iterable valid: "Must be (one of): {valid}".
171+
172+ :param valid: Valid value(s) to include in message. Can be a string, list of values, or None.
173+
174+ :return: Message string indicating valid value(s).
175+ """
176+
182177 if valid is None :
183- msg = ""
184- elif iterable (valid , checklen = True ):
178+ return ""
179+ if isinstance (valid , str ):
180+ return f" Must be: '{ valid } '."
181+ if iterable (valid , checklen = True ):
185182 vstr = "'" + "', '" .join (str (k ) for k in valid ) + "'"
186- msg = f" Must be one of: { vstr } ."
187- else :
188- msg = f" Must be: '{ valid [0 ]} '."
183+ return f" Must be one of: { vstr } ."
189184
190- return msg
185+ return f" Must be: ' { valid [ 0 ] } '."
191186
192187
193188def iterable (value : Any , checklen : bool = False ) -> bool :
0 commit comments