factor out another repeated error checking pattern