refactor error checking using macro