Simply keeping reminded of that exact pattern of events is helpful to avoid sliding into the existential dread side.
I also think it's really important that when you find yourself right there on that edge, pulling your hair out? Stop what you're doing. Walk away for a couple hours. Come back fresh minded. I refer to that as "letting it stew in my brain", and I tell my coworkers and managers that. It emphasizes that my value isn't just the lines of code, but the level of thought too.