The Messenger of Allah ﷺ said, "None of you should wish for death because of some hardship that has befallen him in this world. Rather, let him say, 'Allah, keep me alive as long as life is better for me, and take my soul when death is better for me.'"
أخبرنا قتيبة قال حدثنا يزيد وهو ابن زريع عن حميد
أن رسول الله ﷺ قال لا يتمنين أحدكم الموت لضر نزل به في الدنيا ولكن ليقل اللهم أحيني ما كانت الحياة خيرا لي وتوفني إذا كانت الوفاة خيرا لي
[Its isnad is sahih] Ahmad narrated it (3/104) from the hadith of Humayd Tawil, with the same wording, and he stated the explicit hearing in Kubra, no. 1946. With the explicit hearing, according to Ibn Hibban in his Sahih, no. 2462, and the hadith is in Kubra,