(1) Separate work from life, be able to walk away for a bit when work isn't going well. And do walk away if things aren't going well. If you haven't been able to write any code for the past few hours, odds are you won't be able to write any in the next few hours.
(2) Focus on process over results. IE have a good process to minimize the amount of time you spend thinking about what you should be doing, whether you did the right thing, etc. What honestly helps in these cases is just having a task list of "I need to get XYZ done today" and then blasting through it without leaving room for thinking about things too much. I like Getting Things Done (ie https://www.amazon.com/Getting-Things-Done-Stress-Free-Produ...) because it helps separate work from life.
(3) Take the long view of your life/career. The truth is that you are going to make mistakes, bugs will get into prod, you're going to get burned out, etc, so you need to accept that you will have "bad" days (or days, weeks, moths where you just don't care about what you're doing in which case it is obviously going to be crap) and focus on the process for improving them to minimize them over the long run. I think the important question here isn't "did I make mistakes" but rather "is my process resulting in a slower rate of mistakes/less severe mistakes.
(4) Never forget to eat, sleep, drink water, and exercise. Especially sleep. when things are bad we tend to sacrifice sleep, that almost always makes it worse.
Most of it I think is summarized as having a process you can trust so that when things do go poorly you can focus on the process in those moments. The process will get you out.
Personal time/task management- The classic, Getting Things Done(https://www.amazon.com/Getting-Things-Done-Stress-Free-Produ...). The power this has on people cannot be understated. Turns out that most of how life is conducted is rife with forgetfulness, decision paralysis, prioritization mistakes, and massive motivation issues. This book gives you specific workflows to cut through these in a magical way.
Personal Knowledge Management- The equally classic, How to Take Smart Notes(https://www.amazon.com/How-Take-Smart-Notes-Technique/dp/398...). Where GTD(above) does this for well-defined tasks/work, this book does it for open-ended work, giving you an amazing workflow for introducing "Thinking by Writing", which is frankly a superpower. This lets you see things your friends/colleagues simply won't, lets you deconstruct your feelings better, learn new/deeper subjects faster, and connect thoughts in a way to produce real insight.
For Product/Business Management, Gojko Adzic's "Impact Mapping"(https://www.amazon.com/Impact-Mapping-software-products-proj...) feels like it could make nearly every software team/business 10x better by just reading this book. I've personally watched as enormous portions of my life were spent on things that barely moved the needle for companies, or merely didn't keep the metric from rising. So many projects taken on faith that if you work on X, X will improve, without ever measuring, or asking if you could have accomplished that with less. The world looks insane afterward.