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

Embues the status package with the ability to create statuses
from generic errors that implement the interface:

type StatusError interface {
    Status() *Status
}

This was designed with the github.com/gogo/protobuf project in mind,
but is implemented in a fashion that makes it accessible to arbitrary
payloads.

Fixes #1885.
This commit is contained in:
Johan Brandhorst 2018-03-26 23:33:25 +01:00 committed by dfawley
parent bdb0727fa7
commit 275695638f
2 changed files with 50 additions and 7 deletions

View File

@ -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)}
}
@ -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 {
return se.Status(), true
}
return New(codes.Unknown, err.Error()), false
}
@ -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
}

View File

@ -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"
@ -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")