Skip to content

Commit 1181676

Browse files
committed
Add logic to handle exceptions in the lifecycle transitions
1 parent ec089fd commit 1181676

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

rclpy/rclpy/lifecycle/node.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import traceback
1516
from typing import Any
1617
from typing import Callable
1718
from typing import Dict
@@ -238,7 +239,13 @@ def __transition_callback_impl(self, callback_name: CallbackNames,
238239
else:
239240
raise ValueError(f'Not valid callback name "{callback_name}" given.')
240241

241-
ret = cb(state)
242+
try:
243+
ret = cb(state)
244+
except Exception:
245+
self._logger.error(
246+
f'Caught exception in {callback_name}() callback of managed entity '
247+
f'{entity}:\n{traceback.format_exc()}')
248+
return TransitionCallbackReturn.ERROR
242249
if not isinstance(ret, _rclpy.TransitionCallbackReturnType):
243250
raise TypeError(
244251
f'{callback_name}() return value of class {type(entity)} should be'
@@ -397,7 +404,9 @@ def __execute_callback(
397404
ret = cb(previous_state)
398405
return ret
399406
except Exception:
400-
# TODO(ivanpauno): log sth here
407+
self._logger.error(
408+
f'Caught exception in transition callback for state {current_state_id}:\n'
409+
f'{traceback.format_exc()}')
401410
return TransitionCallbackReturn.ERROR
402411

403412
def __change_state(self, transition_id: int) -> TransitionCallbackReturn:

0 commit comments

Comments
 (0)