I thought about multiple ways of doing this, without as much duplication of logic. However, since the errors are to be logged rather than reported, and such messages should be as meaningful as possible, I determined that this is the best way to do this.