re: How to handle errors in Go? [5 rules] VIEW POST

re: This is kind of the biggest conundrum. If error happens in component or routine, we usually log the error there since log message can provide addit...

Very good, thought through question(s) Oscar.

I think you are asking 2 questions right?

1) Why do you think this is helpful? "Its always better to handle/log the error where it occurs."

IMHO, If you can handle an error, no need to log it, maybe as a warning. If you can't handle it, then you log it.

2) "Is there other better mechanism to avoid this confusion, log error immediately with metadata/state that happened ?"

I like to do this:

if nowNs % 2 > 0 {
        return "", TokenExpiredErr{expiredAt: nowNs}

If I can't handle an error, I create a custom struct, decorated with all the information needed, and return it to the more competent component that can handle it (or can't and logs it).

Let me know if anything is unclear, or you have a concrete example. We can try to "debug it" together.

code of conduct - report abuse