Skip to content

Commit 26737ae

Browse files
authored
improve the error message for invalid message types (#558)
* improve the error message for invalid message types Signed-off-by: Dirk Thomas <[email protected]> * also catch exception for detected message types Signed-off-by: Dirk Thomas <[email protected]>
1 parent 674791c commit 26737ae

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

ros2topic/ros2topic/api/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ def _get_msg_class(node, topic, include_hidden_topics):
141141
# Could not determine the type for the passed topic
142142
return None
143143

144-
return get_message(message_type)
144+
try:
145+
return get_message(message_type)
146+
except (AttributeError, ModuleNotFoundError, ValueError):
147+
raise RuntimeError("The message type '%s' is invalid" % message_type)
145148

146149

147150
class TopicMessagePrototypeCompleter:

ros2topic/ros2topic/verb/echo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ def main(args):
9696
message_type = get_msg_class(
9797
node, args.topic_name, include_hidden_topics=True)
9898
else:
99-
message_type = get_message(args.message_type)
99+
try:
100+
message_type = get_message(args.message_type)
101+
except (AttributeError, ModuleNotFoundError, ValueError):
102+
raise RuntimeError('The passed message type is invalid')
100103

101104
if message_type is None:
102105
raise RuntimeError(

ros2topic/ros2topic/verb/pub.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ def publisher(
129129
keep_alive: float,
130130
) -> Optional[str]:
131131
"""Initialize a node with a single publisher and run its publish loop (maybe only once)."""
132-
msg_module = get_message(message_type)
132+
try:
133+
msg_module = get_message(message_type)
134+
except (AttributeError, ModuleNotFoundError, ValueError):
135+
raise RuntimeError('The passed message type is invalid')
133136
values_dictionary = yaml.safe_load(values)
134137
if not isinstance(values_dictionary, dict):
135138
return 'The passed value needs to be a dictionary in YAML format'

0 commit comments

Comments
 (0)