Just been thinking through some of the mechanical failures I've experienced over the years. Mercifully not that many, really...
Broken spokes - I've had a couple over the years, but not a problem that affects me as much as some others, it seems. Probably down to dumb luck.
Derailleurs - had a Di2 rear mech overshift into the wheel, not sure why. Cage got bent but luckily the hanger snapped before anything more catastrophic broke. Last derailleur problem before that was when one that fell apart mid-ride, requiring me to shorten the chain and ride home single-speed. That was probably down to a poor maintenance/cleaning regime. It was some years ago.
Chain - I've only ever once had a chain snap on me mid-ride. That too was probably down to a poor maintenance/cleaning regime, or maybe just cack-handed shifting. I felt it going before it actually went. Stopped to inspect the cause of the strange skipping and found one of the outer plates almost at right angles to the rest of the chain.
Cranks - had a Brompton LH crank snap on me while pulling away from the lights, sending me tumbling into the road. Inspection of the crank revealed that the crack must have been developing for some time, so maybe I should have caught that before it happened. I also once had the RH crank separate from the chainring, also while pulling away from the lights. That was on the old style Brompton chainset, which was made of cottage cheese.
Cables - on my LEJOG in 2016, I was about 30 miles from JOG when I noticed that the gears were no longer shifting properly. Initial inspection suggested the cable had come loose, so I retensioned and tightened it and carried on. But the problem recurred soon after, so I stopped and investigated further. I discovered that a grommet was missing where the cable exited the chainstay, so the outer was slipping inside the frame, effectively slackening the cable. I have no idea why it waited until almost the very end of the ride to start making itself noticed but shifting had been fine up to that point.
Shifters - had a 105 5700 LH shifter catastrophically fail when a pawl inside the shifter snapped. No idea if there's anything I could have done to prevent that, I suspect it's a design flaw as I've heard of similar failures for other people. It's also a part that you can't buy as a spare, so the whole shifter was scrap. Replaced it with a second-hand Ultegra 6700 shifter that is still going strong.