Sunan an-Nasa'i, Book 21, Hadith 3 (In-Book) ⓘ
It was narrated form Anas that the Messenger of Allah said: "None of you should wish for death because of some harm that befalls him, rather he should say: 'Allahumma ahini ma kanatil-hayatu khairanli wa tawaffani idha kanatil-wafatu khairanli (O Allah, keep me alive so alive so long as life is good for me, and cause me to die when death is good for me.)"'
أَخْبَرَنَا قُتَيْبَةُ، قَالَ حَدَّثَنَا يَزِيدُ، - وَهُوَ ابْنُ زُرَيْعٍ - عَنْ حُمَيْدٍ، عَنْ أَنَسٍ، أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم قَالَ " لاَ يَتَمَنَّيَنَّ أَحَدُكُمُ الْمَوْتَ لِضُرٍّ نَزَلَ بِهِ فِي الدُّنْيَا وَلَكِنْ لِيَقُلِ اللَّهُمَّ أَحْيِنِي مَا كَانَتِ الْحَيَاةُ خَيْرًا لِي وَتَوَفَّنِي إِذَا كَانَتِ الْوَفَاةُ خَيْرًا لِي " .
also referenced under: Hadith 1820 (Reference) & Volume 3, Book 21, Hadith 1821 (DarusSalam)