Skip to content

status: Allow external packages to produce status-compatible errors #1927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (se *statusError) Error() string {
return fmt.Sprintf("rpc error: code = %s desc = %s", codes.Code(p.GetCode()), p.GetMessage())
}

func (se *statusError) status() *Status {
func (se *statusError) Status() *Status {
return &Status{s: (*spb.Status)(se)}
}

Expand Down Expand Up @@ -120,14 +120,14 @@ func FromProto(s *spb.Status) *Status {
}

// FromError returns a Status representing err if it was produced from this
// package. Otherwise, ok is false and a Status is returned with codes.Unknown
// and the original error message.
// package or has a method Status() *Status. Otherwise, ok is false and a
// Status is returned with codes.Unknown and the original error message.
func FromError(err error) (s *Status, ok bool) {
if err == nil {
return &Status{s: &spb.Status{Code: int32(codes.OK)}}, true
}
if se, ok := err.(*statusError); ok {
return se.status(), true
if se, ok := err.(interface{ Status() *Status }); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change the docstring to say "if it was produced from this package or has a method Status() *Status"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return se.Status(), true
}
return New(codes.Unknown, err.Error()), false
}
Expand Down Expand Up @@ -182,8 +182,8 @@ func Code(err error) codes.Code {
if err == nil {
return codes.OK
}
if se, ok := err.(*statusError); ok {
return se.status().Code()
if se, ok := err.(interface{ Status() *Status }); ok {
return se.Status().Code()
}
return codes.Unknown
}
43 changes: 43 additions & 0 deletions status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"reflect"
"testing"

"github.com/golang/protobuf/ptypes/any"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
apb "github.com/golang/protobuf/ptypes/any"
Expand Down Expand Up @@ -119,6 +121,47 @@ func TestFromErrorOK(t *testing.T) {
}
}

type customError struct {
Code codes.Code
Message string
Details []*any.Any
}

func (c customError) Error() string {
return fmt.Sprintf("rpc error: code = %s desc = %s", c.Code, c.Message)
}

func (c customError) Status() *Status {
return &Status{
s: &spb.Status{
Code: int32(c.Code),
Message: c.Message,
Details: c.Details,
},
}
}

func TestFromErrorImplementsInterface(t *testing.T) {
code, message := codes.Internal, "test description"
details := []*any.Any{{
TypeUrl: "testUrl",
Value: []byte("testValue"),
}}
err := customError{
Code: code,
Message: message,
Details: details,
}
s, ok := FromError(err)
if !ok || s.Code() != code || s.Message() != message || s.Err() == nil {
t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q, Err()!=nil>, true", err, s, ok, code, message)
}
pd := s.Proto().GetDetails()
if len(pd) != 1 || !reflect.DeepEqual(pd[0], details[0]) {
t.Fatalf("s.Proto.GetDetails() = %v; want <Details()=%s>", pd, details)
}
}

func TestFromErrorUnknownError(t *testing.T) {
code, message := codes.Unknown, "unknown error"
err := errors.New("unknown error")
Expand Down