- You can add in your handler recovery part
import "runtime/debug"
defer func() {
if err := recover(); err != nil {
log.Errorf("Recovered from err: %v\n %s", err, debug.Stack())
}
}()
- More than that: there are specific middleware for catching such panic: https://github.com/kazegusuri/grpc-panic-handler
From documentation:
import (
panichandler "github.com/kazegusuri/grpc-panic-handler"
)
func main() {
uIntOpt := grpc.UnaryInterceptor(panichandler.UnaryPanicHandler)
sIntOpt := grpc.StreamInterceptor(panichandler.StreamPanicHandler)
grpc.NewServer(uIntOpt, sIntOpt)
}
The second approach is more reliable when recovery handler approach is faster to add