I use pexpect frequently for automating serial-port communications. It’s very useful for implementing test/flashing automation that needs some kind of a serial-port step (such as asking u-boot to run a few commands).
For me, I detect them by checking the exit-code on each command that I run, and also by looking for expected output in response to the commands.
Corrective action is usually a full-stage retry.