In a Sahih Hadith, the Prophet Muhammad, Peace and Blessings of Allah be upon him, his family and companions, said:
“Amazing is the affair of the believer. This is because there is good for him in every matter and this is not the case with anyone other than the believer.
When he is in a state of happiness, he is thankful to Allah, and within that there is good for him, and if he is harmed, then he keeps to being patient and thus there is good for him in that.”
عَجَبًا لِأَمْرِ الْمُؤْمِنِ إِنَّ أَمْرَهُ كُلَّهُ خَيْرٌ وَلَيْسَ ذَاكَ لِأَحَدٍ إِلَّا لِلْمُؤْمِنِ إِنْ أَصَابَتْهُ سَرَّاءُ شَكَرَ فَكَانَ خَيْرًا لَهُ وَإِنْ أَصَابَتْهُ ضَرَّاءُ صَبَرَ فَكَانَ خَيْرًا لَهُ
~ Reference: Sahih Muslim