The Prophet ﷺ said, "None of you should wish for death because of a calamity befalling him; but if he has to wish for death, he should say: "O Allah! Keep me alive as long as life is better for me, and let me die if death is better for me.' " (Using translation from Bukhārī 5671)
أَنَّ رَسُولَ اللهِ ﷺ قَالَ لَا يَتَمَنَّ الْمُؤْمِنُ الْمَوْتَ مِنْ ضُرِّ نَزَلَ بِهِ إِنْ كَانَ لَا بُدَّ فَاعِلًا فَلْيَقُلِ اللهُمَّ أَحْيِنِي مَا كَانَتِ الْحَيَاةُ خَيْرًا لِي وَتَوَفَّنِي مَا كَانَتِ الْوَفَاةُ خَيْرًا لِي