@@ -61,7 +61,7 @@ ClassMethod DivideByZeroPython() [ Language = python ]
6161 stack _trace _list = traceback .format _tb (e .__traceback __)
6262 if stack _trace _list :
6363 last _instruction _line = stack _trace _list [-1 ].strip ()
64- errobj .Location = last _instruction _line
64+ errobj .Location = last _instruction _line
6565 errobj .Log ()
6666 print (" Caught exception: " +str (e ))
6767}
@@ -77,17 +77,99 @@ ClassMethod DivideByZeroPython2() [ Language = python ]
7777 except ZeroDivisionError as e :
7878 tb = e .__traceback __
7979 last _frame = traceback .extract _tb (tb )[-1 ]
80-
80+
8181 # 2 . Extract specific parts
8282 error _name = f " <{type(e).__name__.upper()}>" # e .g ., <NAMEERROR >
8383 line _no = last _frame .lineno # e .g ., 6
8484 func _name = last _frame .name # e .g ., <module > or my _func
85- filename = os .path .basename (last _frame .filename ).replace ('.py ', '')
86-
85+ filename = os .path .basename (last _frame .filename ).replace ('.py ', '')
86+
8787 iris _error = f " {func_name}+{line_no}^{filename}"
8888 errobj =iris .cls (" %Exception.General" )._New (error _name ,2603 ,iris _error )
8989 errobj .Log ()
9090 print (" Caught exception: " +str (e ))
9191}
9292
93+ ClassMethod DivideByZeroPythonWithInCls () [ Language = python ]
94+ {
95+ import traceback
96+ import iris
97+
98+ class MathOperations :
99+ def divide _numbers (self ):
100+ try :
101+ print (1 /0 )
102+ except Exception as e :
103+ tb = e .__traceback __
104+ stack = traceback .extract _tb (tb )[-1 ]
105+
106+ error _name = f " <{type(e).__name__.upper()}>"
107+ cls _name = self .__class __.__name __
108+ func _name = stack [2 ]
109+ line _no = stack [1 ]
110+ # create IRIS format error string
111+ iris _error = f " {func_name}+{line_no}^{cls_name}"
112+ python _exception = 2603
113+ errobj =iris .cls (" %Exception.General" )._New (error _name , python _exception , iris _error )
114+ a =errobj .Log ()
115+ print (" Caught exception: " + str (e ))
116+
117+ obj = MathOperations ()
118+ obj .divide _numbers ()
119+ }
120+
121+ /// Pass the python exception to IRIS class to store the error in error log
122+ ClassMethod PYExecCaptureInIRIS () [ Language = python ]
123+ {
124+ import traceback
125+ import iris
126+
127+ class MathOperations :
128+ def divide _numbers (self ):
129+ try :
130+ print (1 /0 )
131+ except Exception as e :
132+ status = iris .cls (__name __).ConvertPyExceToIRISFormat (e )
133+
134+ obj = MathOperations ()
135+ obj .divide _numbers ()
136+ }
137+ ClassMethod PYFuncExecCaptureInIRIS () [ Language = python ]
138+ {
139+ import traceback
140+ import iris
141+
142+ def divide _numbers ():
143+ try :
144+ print (1 /0 )
145+ except Exception as e :
146+ status = iris .cls (__name __).ConvertPyExceToIRISFormat (e )
147+
148+ divide _numbers ()
149+ }
150+ /// Convert Python Exception to IRIS Exception Format and log it
151+ ClassMethod ConvertPyExceToIRISFormat (pyException )
152+ {
153+ Set pyBuiltins = ##class (%SYS.Python ).Builtins ()
154+ Set ex = pyBuiltins .type (pyException )
155+ Set error = $Property (ex , " __name__" )
156+ Set error = " <" _$ZCVT (error ," U" )_" >"
157+
158+ Set traceback = ##class (%SYS.Python ).Import (" traceback" )
159+
160+ Set tb = $Property (pyException , " __traceback__" )
161+ Set frames = $Method (traceback ," extract_tb" ,tb )
162+ Set lastFrame = $Method (frames ," __getitem__" ,-1 )
163+
164+ Set file = $Property (lastFrame ," filename" )
165+ set line = $Property (lastFrame ," lineno" )
166+ set function = $Property (lastFrame ," name" )
167+ Set code = $Property (lastFrame ," line" )
168+
169+ Set msg = function _" +" _line _" ^" _file
170+
171+ Set generalExcep = ##class (%Exception.General ).%New (error ,2603 ,msg )
172+ Do generalExcep .Log ()
173+ }
174+
93175}
0 commit comments