Make ClearErrorOnReturn a top-level type and create an instance at the top of this function, then you don't have to reshuffle all this code.