David Weber
posts an insightful essay about what can be gotten away with in fiction. He's right that plenty of bad decisions in real life couldn't be dramatized without the reader losing faith in the story. I'm reminded of Hitler invading Russia. He's determined not to make the same mistakes as Napoleon, who lost so many troops on the retreat from Moscow. Yet what does he do? He overstays, he doesn't have his soldiers issued winter uniforms, he loses almost a quarter of his Eastern Army in the first five months of Operation Barbarossa.
I can, though, think of two possible defenses, places where you can have characters make disastrous mistakes. The first is trivial. When time travel is a part of the story, writers can have their characters go back to correct mistakes. The error exists essentially to be corrected.
Defense number two - well, frankly, it's also of limited use, but it's got a noble heritage. This is for the character to acknowledge that they are acting out of character. The most famous usage of this defense was in Casablanca, where Senor Ferrari (Sidney Greenstreet)
says to Laszlo, about the letters of credit: I observe that in one respect you are a very fortunate man, Monsieur. I am moved to make one more suggestion, why, I do not know, because it cannot possibly profit me, but, have you heard about Signor Ugarte and the letters of credit?
This man, who has been portrayed as unrelentingly greedy, even going so far as to offer to 'buy' Sam from Rick, goes on to volunteer the information that Rick is the man for them to see !
The plot revolves around Laszlo and Ilsa finding out where to get the letters of credit (a silly contrivance, letters that the Nazis would 'have' to honor) so they can get away. Ugarte is dead by this point; there's nobody else to point them in Rick's direction. I like that one of the most-honored movies of all time has this lapse of character not just acknowledged, but touted. So the next time you write yourself into a corner, just pull that deus ex machina out of your butt and have said deus say "I don't know why I'm doing this, it's entirely against my nature, but what the hey, you look like a nice kid".