Fix possible NameError in error handling.

Thanks to Filippo Giunchedi for catching this one in a review.