Skip to content

Commit

Permalink
Merge pull request #136 from iory/error-handling
Browse files Browse the repository at this point in the history
Handle KeyboardInterrupt and SystemExit separately and log all other exceptions with stack trace
  • Loading branch information
iory authored Dec 23, 2024
2 parents 5beed62 + 0bfeeca commit 9d796e7
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ros/kxr_controller/python/kxr_controller/serial.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import traceback

import rospy
import serial

Expand Down Expand Up @@ -33,6 +35,9 @@ def serial_call_with_retry(func, *args, max_retries=1, retry_interval=0.1, **kwa
while attempts < max_retries:
try:
return func(*args, **kwargs)
except (KeyboardInterrupt, SystemExit):
# Re-raise these exceptions to allow program termination
raise
except (
serial.SerialException,
OSError,
Expand All @@ -43,5 +48,11 @@ def serial_call_with_retry(func, *args, max_retries=1, retry_interval=0.1, **kwa
rospy.logerr(f"[{func.__name__}] Error: {e}")
attempts += 1
rospy.sleep(retry_interval)
except Exception as e:
# Catch all other exceptions and log them
rospy.logerr(f"[{func.__name__}] Exception occurred: {e}")
rospy.logerr(f"Stack trace:\n{traceback.format_exc()}")
attempts += 1
rospy.sleep(retry_interval)
rospy.logerr(f"[{func.__name__}] Failed after {max_retries} attempts.")
return None

0 comments on commit 9d796e7

Please sign in to comment.