Handle better state errors

This fixes #9983.