optimize: a better way for return error
This commit is contained in:
parent
b78060a571
commit
4cba1b3ec9
15
grpc.go
15
grpc.go
@ -343,27 +343,22 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service,
|
||||
}
|
||||
|
||||
// define the handler func
|
||||
fn := func(ctx context.Context, req server.Request, rsp interface{}) error {
|
||||
ch := make(chan error, 1)
|
||||
defer close(ch)
|
||||
|
||||
fn := func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Log("panic recovered: ", r)
|
||||
log.Logf(string(debug.Stack()))
|
||||
ch <- errors.InternalServerError("go.micro.server", "panic recovered: %v", r)
|
||||
err = errors.InternalServerError("go.micro.server", "panic recovered: %v", r)
|
||||
}
|
||||
}()
|
||||
returnValues = function.Call([]reflect.Value{service.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
|
||||
|
||||
// The return value for the method is an error.
|
||||
if err := returnValues[0].Interface(); err != nil {
|
||||
ch <- err.(error)
|
||||
if rerr := returnValues[0].Interface(); rerr != nil {
|
||||
err = rerr.(error)
|
||||
}
|
||||
|
||||
ch <- nil
|
||||
|
||||
return <-ch
|
||||
return err
|
||||
}
|
||||
|
||||
// wrap the handler func
|
||||
|
Loading…
Reference in New Issue
Block a user