Fixing a couple locations that could potentially raise NameErrors
instead of intended exceptions. These were found by looking at
flake8 F821 (undefined name) errors.
Added in unit tests to verify NameErrors would not be raised.
Change-Id: I8619cb0be495b814335a5aea23daca025484d3c6