Skip to content

Commit d9fcf20

Browse files
authored
Merge pull request #13 from AshokThangavel/py-cls-exce-code-sample
CodeSample: add Python error handling by extracting last traceback fr…
2 parents 63296da + cfe5534 commit d9fcf20

File tree

1 file changed

+86
-4
lines changed

1 file changed

+86
-4
lines changed

src/dc/python/test.cls

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)