[CB-2213] Added specific expected error checking.

Also did some refactoring.