19 But Joseph said to them, “Do not fear, for am I in the place of God? 20 As for you, you meant evil against me, but God meant it for good, to bring it about that many people should be kept alive, as they are today. 21 So do not fear; I will provide for you and your little ones.” Thus he comforted them and spoke kindly to them. – Genesis 50:19-21
We take this as kind of a general principle from Scripture, mostly because Paul affirmed it in Romans 8 and reminded us that God works all things together for good for those that love Him and are called according to His purpose. Joseph is the most vivid portrayal of that work of God, but I am pretty sure that it was easier for him to come to that understanding while sitting as second-in-command only to Pharaoh than it was when he was sitting in the prison forgotten by the cupbearer. Sometimes we forget that part. It is much easier to see the good in the rear view mirror after we have already been through the all things part of it to get there. Trust in God to bring you through according to His time and His plan.