I understand you are trying to preempt the error codes but that as pointed out previously depends on your system.
I found this library http://www.ni.com/example/31253/en/ after doing pretty much what you did, implement my own errorhandling with lot of the functionality that this library provide off the shelf. It also allows you to define a range of error codes to trap etc. which may be useful in your case.