Anas (May Allah be pleased with him) reported that:
The Messenger of Allah (ﷺ) said, "Let not one of you wish for death because of a misfortune which befalls him. If he cannot help doing so, he should say: 'O Allah, keep me alive as long as You know that life is better for me, and make me die when death is better for me".[Al- Bukhari and Muslim].
Arabic/English book reference: Book 1, Hadith 40