mirror of https://github.com/grpc/grpc-go.git
Fix nil pointer dereferences from status.FromProto(nil) (#1211)
This commit is contained in:
parent
fc5d355228
commit
84cd50a2f3
|
|
@ -71,16 +71,25 @@ type Status struct {
|
|||
|
||||
// Code returns the status code contained in s.
|
||||
func (s *Status) Code() codes.Code {
|
||||
if s == nil || s.s == nil {
|
||||
return codes.OK
|
||||
}
|
||||
return codes.Code(s.s.Code)
|
||||
}
|
||||
|
||||
// Message returns the message contained in s.
|
||||
func (s *Status) Message() string {
|
||||
if s == nil || s.s == nil {
|
||||
return ""
|
||||
}
|
||||
return s.s.Message
|
||||
}
|
||||
|
||||
// Proto returns s's status as an spb.Status proto message.
|
||||
func (s *Status) Proto() *spb.Status {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
return proto.Clone(s.s).(*spb.Status)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -65,6 +65,24 @@ func TestFromToProto(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFromNilProto(t *testing.T) {
|
||||
tests := []*Status{nil, FromProto(nil)}
|
||||
for _, s := range tests {
|
||||
if c := s.Code(); c != codes.OK {
|
||||
t.Errorf("s: %v - Expected s.Code() = OK; got %v", s, c)
|
||||
}
|
||||
if m := s.Message(); m != "" {
|
||||
t.Errorf("s: %v - Expected s.Message() = \"\"; got %q", s, m)
|
||||
}
|
||||
if p := s.Proto(); p != nil {
|
||||
t.Errorf("s: %v - Expected s.Proto() = nil; got %q", s, p)
|
||||
}
|
||||
if e := s.Err(); e != nil {
|
||||
t.Errorf("s: %v - Expected s.Err() = nil; got %v", s, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
err := Error(codes.Internal, "test description")
|
||||
if got, want := err.Error(), "rpc error: code = Internal desc = test description"; got != want {
|
||||
|
|
|
|||
Loading…
Reference in New Issue