Be always Joyful
God is in control of everything, there are no accidents with God, and in everything is working for what is best for you out of his love you.
Why are there trials? There are at least four reasons trials:
to show your love for him by persevering in the trial and by this judging Satan because you, who have not seen God love him in spite what happens when Satan, who has seen God, rejected him.
To draw you back to where you should be your Christian walk when you have slipped off the path God has prepared you know that you will restore the salvation relation with Jesus and the Father.
To prepare you great deeper relationship with Jesus and the Father so that God can use you for greater service.
To show Satan you are worthy of the relationship you have God and the ministry God has given you as well is all that flows from that relationship and ministry.
As you can see, God is working for your good even when things seem hopeless. Remember he is building you for eternity and not to please the requirements of the wall controlled by Satan.
So when things go wrong be content, God is in charge working for your best regardless of what appears to be happening.