You can make Expect wait for two (or more) different strings at once.
expect {
"Thing for success" {
# little script to handle the success case
puts "hooray, going to do next step"
}
"Thing for failure 1" {
puts "oh dear; didn't work"
exit 1
}
"Thing for failure 2" {
send "something to try to recover\r"
exp_continue; # Try this expect again
}
}
Of course, you'll need to pick appropriate things to match against (check out using glob or regex matching for this) and you'll need to work out how to actually handle the error cases (your choices are usually variations on “ignore”, “fail out” and “try to recover”). Also consider putting in explicit handling of timeouts.