Djarvur / go-err113

Golang linter to check the errors handling expressions
MIT License
45 stars 4 forks source link

Handling panic errors #19

Closed hi10drasingh closed 2 years ago

hi10drasingh commented 2 years ago

How to make panic errors wrapped static

if rec := recover(); rec != nil {
    trace := debug.Stack()

    err = fmt.Errorf("PANIC [%v] TRACE[%s]", rec, string(trace))
}

linter error

err113: do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"PANIC [%v] TRACE[%s]\", rec, string(trace))"
onokonem commented 2 years ago

something like this

// on package level
var ErrPanic = errors.New("PANIC")

// in your code
if rec := recover(); rec != nil {
    trace := debug.Stack()

    err = fmt.Errorf("[%v] TRACE[%s]: %w", rec, string(trace), ErrPanic)
}

BTW, you do not need error for panic.

hi10drasingh commented 2 years ago

Thanks, it worked!

The above code was from a recovery middleware on top of a rest server, that middleware will catch any handler panic and send panic with trace to the outer handler that will log error and will send 500 to the client without breaking the server