Skip to content

Commit

Permalink
status: optimize GRPCStatus() calls (#6539)
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k authored Aug 15, 2023
1 parent 402ba09 commit 3e92504
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,27 @@ func FromError(err error) (s *Status, ok bool) {
}
type grpcstatus interface{ GRPCStatus() *Status }
if gs, ok := err.(grpcstatus); ok {
if gs.GRPCStatus() == nil {
grpcStatus := gs.GRPCStatus()
if grpcStatus == nil {
// Error has status nil, which maps to codes.OK. There
// is no sensible behavior for this, so we turn it into
// an error with codes.Unknown and discard the existing
// status.
return New(codes.Unknown, err.Error()), false
}
return gs.GRPCStatus(), true
return grpcStatus, true
}
var gs grpcstatus
if errors.As(err, &gs) {
if gs.GRPCStatus() == nil {
grpcStatus := gs.GRPCStatus()
if grpcStatus == nil {
// Error wraps an error that has status nil, which maps
// to codes.OK. There is no sensible behavior for this,
// so we turn it into an error with codes.Unknown and
// discard the existing status.
return New(codes.Unknown, err.Error()), false
}
p := gs.GRPCStatus().Proto()
p := grpcStatus.Proto()
p.Message = err.Error()
return status.FromProto(p), true
}
Expand Down

0 comments on commit 3e92504

Please sign in to comment.