The Messenger of Allah ﷺ said, "When calamity strikes any of you, let him say, 'Indeed, we belong to Allah, and indeed, to Him we shall return. O Allah, with You I seek reward for my calamity, so reward me for it and replace it for me with something better.'"
حدثنا موسى بن إسمعيل حدثنا حماد أخبرنا ثابت عن ابن عمر بن أبي سلمة عن أبيه
قال رسول الله ﷺ إذا أصابت أحدكم مصيبة فليقل إنا لله وإنا إليه راجعون اللهم عندك أحتسب مصيبتي فآجرني فيها وأبدل لي بها خيرا منها
[Hasan] Ahmad narrated it, 6/317, and Nasai in Kubra, no. 10910, and Amal al-Yawm wal-Layla, no. 1071, from the hadith of Hammad bin Salama, through this route. Hakim graded it sahih, 4/16, 17, and Dhahabi agreed with him. The hadith has corroborating narrations in Muslim, no. 918, and elsewhere.