Collecting error messages generated during fact gathering

I’m writing a playbook to install a software on 5000 machines. The hosts are added to a group dynamically and the later jobs are running on each host in the group. My question is, during the fact gathering process, if some of the hosts are not responding or something wrong happened which caused the fact gathering failed. I want to collect the error messages and use these messages in the later plays.

I’m wondering how to implement it. An example code would be very appreciated.