Detecting an error in Net::SSH#exec!

When I execute a remote command with Net::SSH#exec!, how do I tell
whether
or not the command succeeded?

So far, this is the way I’ve found to do it:

Net::SSH.start(“host”, “username”) do | ssh |
ssh.exec!(“some_command”) do | channel, stream, data |
if stream == :stderr
# an error occurred
else
# do something with data
end
end

This only works if the command I’m running writes to stderr if and only
if
it encounters an error, which seems flaky.

How do others deal with this?

Thanks,
Randy