179179 "empty_init" ,
180180 "is_Ok" ,
181181 "is_Err" ,
182+ "Err_msg" ,
183+ "Err_code" ,
184+ "Err_traceback" ,
182185 "raised" ,
183186 "expect" ,
184187 "expect_Err" ,
@@ -801,25 +804,41 @@ class Result:
801804 is_Ok (bool): True if the result is a success.
802805 is_Err (bool): True if the result is an error.
803806
804- Methods:
807+ Err_msg (list[str]):
808+ For the Ok(value) variant, returns `[]`.
809+ For the Err(error) variant, returns list of error messages.
810+ Equivalent to `Err(error).unwrap().msg`
805811
806- raised(error_msg="", exception=None):
807- If Ok variant, then returns Ok(value);
808- If Err variant, then raise Err and optionally include `from exception`.
812+ Err_code (list[int]):
813+ For the Ok(value) variant, returns `[]`.
814+ For the Err(error) variant, returns list of error codes.
815+ Equivalent to `Err(error).unwrap().code`
809816
810- expect(error_msg=""):
811- If Ok variant, then returns value from Ok(value);
812- If Err variant, then raise Err and optionally append error_msg and error_code to Err.
817+ Err_traceback (list[list[str]]):
818+ For the Ok(value) variant, returns `[]`.
819+ For the Err(error) variant, returns list of traceback lines.
820+ Equivalent to `Err(error).unwrap().traceback_info`
813821
814- expect_Err(ok_msg=""):
815- If Ok variant, then raise ResultErr message;
816- If Err variant, then returns error from Err(error). error is of type ResultErr.
822+ Methods:
817823
818824 unwrap():
819825 Return the wrapped value in Ok(value) or error in Err(error).
820826
821827 unwrap_or(default):
822- Return the wrapped value in Ok(value) or default.
828+ Return the wrapped value in Ok(value) or return default.
829+
830+ expect(error_msg=""):
831+ If Ok variant, then returns value from value;
832+ If Err variant, then raise Err and optionally append error_msg to it.
833+
834+ expect_Err(ok_msg=""):
835+ If Ok variant, then raise ResultErr(ok_msg);
836+ If Err variant, then returns error in Err(error), which is type ResultErr.
837+
838+ raised(error_msg="", exception=None):
839+ If Ok variant, then returns Ok(value);
840+ If Err variant, then raise Err and optionally include `from exception`.
841+ Useful for check during chained operations
823842
824843 is_Ok_and(ok_func, *args, **kwargs):
825844 True if Ok(value) variant and ok_func(value, *args, **kwargs) returns True,
@@ -892,8 +911,15 @@ class Result:
892911 Return the new Result. If value is not a ResultErr type, then returns Ok(value);
893912 otherwise, returns Err(value).
894913
895- copy():
914+ copy(deepcopy=False ):
896915 Create a copy of the Result.
916+ If deepcopy=True, the returns Result(deepcopy(value)).
917+
918+ register_code(code, description, error_code_group=None):
919+ Register a specific code and description for the group number error_code_group.
920+
921+ error_code(code=None, error_code_group=None):
922+ Get an error code and description for a specific code group.
897923
898924 Example Usage:
899925 >>> result = Result.Ok("Success")
@@ -983,15 +1009,31 @@ def is_Err(self):
9831009 self ._empty_error ()
9841010 return not self ._success
9851011
986- def raised (self , error_msg = "" , exception : Exception = None ):
1012+ @property
1013+ def Err_msg (self ):
1014+ if self ._success :
1015+ return []
1016+ return self ._Err .msg
1017+
1018+ @property
1019+ def Err_code (self ):
1020+ if self ._success :
1021+ return []
1022+ return self ._Err .code
1023+
1024+ @property
1025+ def Err_traceback (self ):
1026+ if self ._success :
1027+ return []
1028+ return self ._Err .traceback_info
1029+
1030+ def unwrap (self ):
9871031 self ._empty_error ()
988- if not self ._success :
989- if error_msg != "" :
990- self .add_Err_msg (error_msg , 1 , add_traceback = False )
991- if exception is None :
992- raise self ._Err
993- raise self ._Err from exception
994- return self
1032+ return self ._Ok if self ._success else self ._Err
1033+
1034+ def unwrap_or (self , default ):
1035+ self ._empty_error ()
1036+ return self ._Ok if self ._success else default
9951037
9961038 def expect (self , error_msg = "" , error_code = 5 ): # 5 -> ResultErr.error_code("Expect")
9971039 self ._empty_error ()
@@ -1009,13 +1051,19 @@ def expect_Err(self, ok_msg="", error_code=5): # 5 -> ResultErr.error_code("Exp
10091051 err .append (ok_msg , add_traceback = False )
10101052 raise ResultErr (err )
10111053
1012- def unwrap (self ):
1054+ def raised (self , error_msg = "" , exception : Exception = None ):
10131055 self ._empty_error ()
1014- return self ._Ok if self ._success else self ._Err
1056+ if not self ._success :
1057+ if error_msg != "" :
1058+ self .add_Err_msg (error_msg , 1 , add_traceback = False )
1059+ if exception is None :
1060+ raise self ._Err
1061+ raise self ._Err from exception
1062+ return self
10151063
1016- def unwrap_or (self , default ):
1064+ def is_Ok_and (self , bool_ok_func , * args , ** kwargs ):
10171065 self ._empty_error ()
1018- return self ._Ok if self ._success else default
1066+ return self ._success and bool_ok_func ( self ._Ok , * args , ** kwargs )
10191067
10201068 def apply (self , ok_func , * args , ** kwargs ):
10211069 self ._empty_error ()
@@ -1104,16 +1152,18 @@ def iter(self):
11041152 return iter ([self ._Ok ])
11051153 return iter ([])
11061154
1107- def is_Ok_and (self , bool_ok_func , * args , ** kwargs ):
1108- self ._empty_error ()
1109- return self ._success and bool_ok_func (self ._Ok , * args , ** kwargs )
1110-
1111- def copy (self , deepcopy = False ):
1112- if self ._success is None :
1113- return Result .empty_init ()
1114- if self ._success and deepcopy :
1115- return Result (_deepcopy (self ._Ok ), error_code_group = self ._g )
1116- return Result (self )
1155+ def add_Err_msg (self , error_msg , error_code = 1 , add_traceback = True ):
1156+ """Convert to error status and append error message and code."""
1157+ if self ._success :
1158+ if error_msg == "" and self ._Ok == "" :
1159+ error_msg = EMPTY_ERROR_MSG
1160+ elif error_msg == "" :
1161+ error_msg = str (self ._Ok )
1162+ self ._success = False
1163+ self ._Ok = ""
1164+ self ._Err = ResultErr ("" , 1 , self ._g )
1165+ if error_msg != "" :
1166+ self ._Err .append (error_msg , error_code , add_traceback , _levels = - 4 )
11171167
11181168 def update_result (self , value , create_new = False , deepcopy = False ):
11191169 if create_new :
@@ -1130,18 +1180,12 @@ def update_result(self, value, create_new=False, deepcopy=False):
11301180 self ._Err = ResultErr ()
11311181 return self
11321182
1133- def add_Err_msg (self , error_msg , error_code = 1 , add_traceback = True ):
1134- """Convert to error status and append error message and code."""
1135- if self ._success :
1136- if error_msg == "" and self ._Ok == "" :
1137- error_msg = EMPTY_ERROR_MSG
1138- elif error_msg == "" :
1139- error_msg = str (self ._Ok )
1140- self ._success = False
1141- self ._Ok = ""
1142- self ._Err = ResultErr ("" , 1 , self ._g )
1143- if error_msg != "" :
1144- self ._Err .append (error_msg , error_code , add_traceback , _levels = - 4 )
1183+ def copy (self , deepcopy = False ):
1184+ if self ._success is None :
1185+ return Result .empty_init ()
1186+ if self ._success and deepcopy :
1187+ return Result (_deepcopy (self ._Ok ), error_code_group = self ._g )
1188+ return Result (self )
11451189
11461190 def register_code (self , code , description , error_code_group = None ):
11471191 """
@@ -1274,17 +1318,16 @@ def __getattr__(self, name):
12741318 Returns:
12751319 The result of the attribute wrapped as a Result or modifies underlying value.
12761320 """
1277-
1278- if self .is_Err :
1279- self .add_Err_msg (f"VAR.{ name } with VAR as Err variant" , self .error_code ("Attribute_While_Error_State" ))
1280- return self
12811321 if name in EXCLUDE_ATTRIBUTES :
12821322 self .add_Err_msg (
12831323 f"{ name } is an excluded attribute/method. Did you forget () on a method or put () on an attrib. If Ok(x.{ name } ) is what you want, then do Ok(x).expect().{ name } " ,
12841324 self .error_code ("Attribute" ),
12851325 )
12861326 return self
12871327
1328+ if self .is_Err :
1329+ self .add_Err_msg (f"VAR.{ name } with VAR as Err variant" , self .error_code ("Attribute_While_Error_State" ))
1330+ return self
12881331 try :
12891332 # Forward any unknown attribute to value in Ok(value) component
12901333 attr = getattr (self ._Ok , name )
0 commit comments