You cannot prevent every setback. However, you can make it a priority to recover quickly from them and get back on track. In every effort there will be interruptions, disappointments, even some disasters.
Your recovery time depends largely on what you decide to make of it. The time and energy you put into anger and self pity will serve mainly to prolong the time it takes to get going again. Basically that's it. Pity time is wasted time.
Accept that the setbacks will come and when they do, decide to move quickly past them. Find something positive in each regression, and that will serve as fuel to get you focused again on progressing toward your goal.
Winners and losers both stumble. The winners are the ones who quickly get back up and keep on going.