My high school English teacher, in her creative writing class, had a rule that we couldn't end a short story with someone dying. She said that having a character that your readers cared about die was a cheap way to create an emotional response in your readers; we were expected to be better writers than that. I immediately wrote a story in which the only character in the story died at the end, in fact, the character --a wounded sailor trapped on a sinking ship-- was dying the entire story. The teacher gave me an "A" because she understood I wasn't using the sailor's death to create an emotional response, nor was it a simple plot device to end a story I had no better way to end.
Culturally, we seem to believe that 'serious' literature doesn't allow happy endings. If Tess doesn't hang at the end of Thomas Hardy's Tess of the D'Urbervilles, the reader misses an indescribable experience of emotional release. Thankfully, I'm not Thomas Hardy, and my goal isn't to write tragedies. That's not to say I don't want to devastate the reader on occasion, but that is not how I chose to leave my readers. I believe in redemption, for characters --if they earn it-- and for the reader.
That doesn't mean that a character the reader loves will never die. Death is inescapable, in real life and in literature. If the reader loves a character enough that they believe in that character as a person, then they already can foresee that death is the ultimate outcome. Which means that death isn't the worst thing that can happen to a character. Years ago, I had a friend who died of cancer in his forties. He had certain regrets, professionally most of all, and knew that some people perceived him a screw up. The cancer felt so unfair, on top of everything else, but not for him. He found himself in that struggle, found that he could handle every bit of it, including his fears, and found peace in the unknown outcome. I've never seen a person change so much by not changing at all, by doubling down on who he was and loving himself. Watching him move through that experience changed me because I saw him find himself, grow confident in who he was and leave this life a peaceful, happy man.
Maybe that's the essence of Tess. We mourn the death but celebrate the life. Perhaps in every situation there is a glimmer of hope, something we can focus on to see into the mist and gloom and beyond what is directly in front of us. Those are the happy endings I like to write.