The Shulchan Aruch · Orach Chayim
אורח חיים רכ״בOrach Chayim 222
Siman 222 of 697 in Orach Chayim — 4 se'ifim
הלכות שאר ברכותLaws of Other Blessings· siman 222
Se’if 1א׳
עַל שְׁמוּעוֹת שֶׁהֵם טוֹבוֹת לוֹ לְבַדּוֹ, מְבָרֵךְ: שֶׁהֶחֱיָנוּ; וְאִם הֵן טוֹבוֹת לוֹ וְלַאֲחֵרִים, מְבָרֵךְ: הַטּוֹב וְהַמֵּטִיב.
On news that are good for him, he makes the blessing of "Shehecheyanu"; If the news is good for him and for others, he makes the blessing of "Hatov VehaMaytiv".
Se’if 2ב׳
עַל שְׁמוּעוֹת רָעוֹת מְבָרֵךְ: בָּרוּךְ אַתָּה ה' אֱלֹהֵינוּ מֶלֶךְ הָעוֹלָם דַּיַּן הָאֱמֶת.
On bad news he makes the blessing: Blessed are you Hashem, Our G-d, Master of the Universe, the judge of truth.”
Se’if 3ג׳
חַיָּב אָדָם לְבָרֵךְ עַל הָרָעָה בְּדַעַת שְׁלֵמָה וּבְנֶפֶשׁ חֲפֵצָה, כְּדֶרֶךְ שֶׁמְּבָרֵךְ עַל הַטּוֹבָה, כִּי הָרָעָה לְעוֹבְדֵי הַשֵּׁם הִיא שִׂמְחָתָם וְטוֹבָתָם, כֵּיוָן שֶׁמְּקַבֵּל מֵאַהֲבָה מַה שֶּׁגָּזַר עָלָיו הַשֵּׁם. נִמְצָא שֶׁבְּקַבָּלַת רָעָה זוֹ הוּא עוֹבֵד אֶת הַשֵּׁם, שֶׁהִיא שִׂמְחָה לוֹ.
A person is obligated to bless on the bad with a full mind and wanting soul, in the way that he blesses happily on the good, because the bad, for servants of G-d, is their happiness and goodness. Since he/she has accepted out of love what G-d has decreed, he/she finds that by accepting this bad, he/she is serving G-d which bring him happiness.
Shulchan Aruch, Orach Chayim — Torat Emet 363 edition, via Sefaria — public domain
English: Sefaria Community Translation — CC0