The Prophet ﷺ came out upon a group of his companions who were laughing and chatting. He said, "By the One in whose hand my soul rests, if you knew what I know, you would laugh little and weep much."Then he turned away, and the people wept. Allah revealed to him, "Muhammad, why do you drive my servants to despair?"So the Prophet ﷺ returned and said, "Rejoice, be steadfast, and seek the middle path."
خرج النبي ﷺ على رهط من أصحابه يضحكون ويتحدثون، فقال: والذي نفسي بيده، لو تعلمون ما أعلم لضحكتم قليلا، ولبكيتم كثيرا، ثم انصرف وأبكى القوم، وأوحى الله عز وجل إليه: يا محمد، لم تقنط عبادي؟ فرجع النبي ﷺ فقال: أبشروا، وسددوا وقاربوا
Bukhari related it in Adab al-Mufrad (254), Ibn Hibban (113), and Bayhaqi in Shuab (1027), all of them through Rabi bin Muslim, who said, "Muhammad bin Ziyad narrated to us, from Abu Hurairah," and he related it. Its chain of transmission is sahih.Bayhaqi said, "This shows that one's fear should not be such that it drives him to despair and makes him lose hope in Allah's mercy, just as one's hope should not be such that it makes him feel secure from Allah's plan, or emboldens him to disobey Allah."