I found this really good article about how to handle yourself during a mid-race catastrophe, while not ideal I've always been a planner and often will have multiple back up plans or goals (for good or bad) in my head for races. A prime example was during my first marathon.
I had an A, B and C goal just in case. This worked out well as when my A goal came unstuck I had a fall back position which I knew I could lean into and rely on. Luckily that confidence brought my wobble back from the brink into the straight and narrow and I was able to come back and claw my way back to my A goal.
Read on if you want to learn some more about strategies you can employ in your races...