The mishna articulates a general principle: One is obligated to recite a blessing for the bad that befalls him just as he recites a blessing for the good that befalls him, as it is stated: “And you shall love the Lord your God with all your heart, with all your soul, and with all your might” (Deuteronomy 6:5). The mishna explains this verse as follows: “With all your heart” means with your two inclinations, with your good inclination and your evil inclination, both of which must be subjugated to the love of God. “With all your soul” means even if God takes your soul. “And with all your might” means with all your money, as money is referred to in the Bible as might. Alternatively, it may be explained that “with all your might” means with every measure that He metes out to you; whether it is good or troublesome, thank Him.
Berachot 9:5
(Sefaria)