Go-micro: why grpc server exec recover(). not return error ?

Created on 1 Nov 2019  路  9Comments  路  Source: asim/go-micro

code in "github.com/micro/[email protected]/server/grpc/grpc.go:340"

    fn := func(ctx context.Context, req server.Request, rsp interface{}) error {
        defer func() {
            if r := recover(); r != nil {
                log.Logf("handler %s panic recovered, err: %s", mtype.method.Name, r)
            }
        }()
        .....

        return nil
    }

I think should return error.
Thanks for answering my question.

RPC enhancement question

All 9 comments

How would we do that?

Let development handle returns errors.Otherwise, it's hard to tell the difference between no data and errors.
like
fn := func(ctx context.Context, req server.Request, rsp interface{}) (e error) {

        defer func() {
            if r := recover(); r != nil {
                log.Logf("handler %s panic recovered, err: %s", mtype.method.Name, r)
                e = errors.InternalServerError("go.micro.server", "panic recovered: %v", r)
            }
        }()
        ....

        return
    }

Feel free to submit a PR

I think the motivation to recover from the panic is fine (maybe a good idea). Something similar is implemented here: https://github.com/micro/go-micro/blob/master/server/rpc_server.go#L90

I have submitted a PR to improve this: https://github.com/micro/go-micro/pull/906

I saw the PR has been merged and released. Can we close this issue?

Thanks for your help

@aimpsmile can you close this issue?

hi all , i am still confused why just recover() after panic, but not return error?
could you explain more, plz?

yes, i think that we need fix that

Was this page helpful?
0 / 5 - 0 ratings