Skip to content

Commit fbb411f

Browse files
committed
feat: result.Err_msg_contains added ignore_case arg
1 parent 3d60fe1 commit fbb411f

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

ResultContainer/__init__.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ def copy(self):
466466
return ResultErr(self)
467467

468468
def clear(self):
469-
"""Clear all stored error messages and reset the instance to non-error status."""
469+
"""
470+
Clear the error message and traceback lists.
471+
This resets the instance to non-error status.
472+
"""
470473
self.msg.clear()
471474
self.traceback_info.clear()
472475

@@ -481,19 +484,26 @@ def contains_msg(self, msg: str):
481484
"""Check if a specific message exists in the error messages."""
482485
return msg in self.msg
483486

484-
def contains(self, sub_msg: str) -> bool:
487+
def contains(self, sub_msg: str, ignore_case: bool = False) -> bool:
485488
"""Check if any of the error messages contain the sub_msg.
486489
487490
Args:
488-
sub_msg (str): String to search for in the error messages.
491+
sub_msg (str): String to search for in the error messages.
492+
ignore_case (bool): Set to true to ignore case in the matching.
489493
490494
Returns:
491495
bool: True if sub_msg is found, otherwise False.
492496
"""
493-
for msg in self.msg:
494-
if sub_msg in msg:
495-
return True
496-
return Falsegw
497+
if ignore_case:
498+
sub_msg = sub_msg.lower()
499+
for msg in self.msg:
500+
if sub_msg in msg.lower():
501+
return True
502+
else:
503+
for msg in self.msg:
504+
if sub_msg in msg:
505+
return True
506+
return False
497507

498508
def str(self, sep: str = " | ", as_repr: bool = True, add_traceback: bool = False) -> str:
499509
"""Return a string representation of the error messages.
@@ -776,7 +786,7 @@ class Result:
776786
unless type(item) is ResultErr, then returns Err(item).
777787
If expand is True, then returns `list(iter_wrap())`.
778788
779-
Err_msg_contains(sub_msg: str):
789+
Err_msg_contains(sub_msg: str, ignore_case: bool=False):
780790
Returns true if Err(e) variant and sub_msg is contained in
781791
any of the error messages. The Ok variant returns False.
782792
@@ -1041,7 +1051,7 @@ def apply_map(self, ok_func, unwrap: bool = False):
10411051
err.add_Err_msg(f"{type(e).__name__}: {e}", False)
10421052
else:
10431053
err = Result(self._val)
1044-
err.add_Err_msg("Result.apply_map on Err")
1054+
err.add_Err_msg("Result.apply_map on Err", _levels=-4)
10451055
return err
10461056

10471057
def map(self, ok_func):
@@ -1089,18 +1099,19 @@ def iter(self, unwrap: bool = True, expand: bool = False):
10891099
return self.iter_unwrap(expand)
10901100
return self.iter_wrap(expand)
10911101

1092-
def Err_msg_contains(self, sub_msg: str) -> bool:
1102+
def Err_msg_contains(self, sub_msg: str, ignore_case: bool = False) -> bool:
10931103
"""
10941104
Returns true if Err(e) variant and sub_msg is contained in any of
10951105
the error messages. The Ok variant returns False.
10961106
10971107
Args:
1098-
sub_msg (str): String to search for in the error messages.
1108+
sub_msg (str): String to search for in the error messages.
1109+
ignore_case (bool): Set to true to ignore case in the matching.
10991110
11001111
Returns:
11011112
bool: True if sub_msg is found, otherwise False.
11021113
"""
1103-
return False if self._success else self._val.contains(sub_msg)
1114+
return False if self._success else self._val.contains(sub_msg, ignore_case)
11041115

11051116
def add_Err_msg(self, error_msg, add_traceback: bool = True, *, _levels=-3):
11061117
"""Convert to error status and append error message."""

0 commit comments

Comments
 (0)