Skip to content

Commit b50e71d

Browse files
committed
Enhance divide by zero error handling in Python methods with detailed traceback logging
1 parent 037668a commit b50e71d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ dataclasses-json
44
sqlalchemy-iris
55
flask
66
requests
7+
traceback2

src/dc/python/test.cls

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,42 @@ ClassMethod DivideByZero() As %Status
5252
ClassMethod DivideByZeroPython() [ Language = python ]
5353
{
5454
import sample
55+
import traceback
5556
import iris
5657
try:
5758
print("divide by zero=" + str(sample.dividezero(1)))
5859
except ZeroDivisionError as e:
5960
errobj=iris.cls("%Exception.General")._New(str(e),42)
60-
a=errobj.Log()
61-
print("Caught exception: " + str(e))
61+
stack_trace_list = traceback.format_tb(e.__traceback__)
62+
if stack_trace_list:
63+
last_instruction_line = stack_trace_list[-1].strip()
64+
errobj.Location = last_instruction_line
65+
errobj.Log()
66+
print("Caught exception: " +str(e))
67+
}
68+
69+
ClassMethod DivideByZeroPython2() [ Language = python ]
70+
{
71+
import sample
72+
import traceback
73+
import iris
74+
import os
75+
try:
76+
print("divide by zero=" + str(sample.dividezero(1)))
77+
except ZeroDivisionError as e:
78+
tb = e.__traceback__
79+
last_frame = traceback.extract_tb(tb)[-1]
80+
81+
# 2. Extract specific parts
82+
error_name = f"<{type(e).__name__.upper()}>" # e.g., <NAMEERROR>
83+
line_no = last_frame.lineno # e.g., 6
84+
func_name = last_frame.name # e.g., <module> or my_func
85+
filename = os.path.basename(last_frame.filename).replace('.py', '')
86+
87+
iris_error = f"{func_name}+{line_no}^{filename}"
88+
errobj=iris.cls("%Exception.General")._New(error_name,2603,iris_error)
89+
errobj.Log()
90+
print("Caught exception: " +str(e))
6291
}
6392

6493
}

0 commit comments

Comments
 (0)