chore: remove unreferenced kit/grpc package

pull/19846/head
Mark Rushakoff 2020-10-28 12:38:01 -04:00
parent 78cafa861b
commit 10c32e4079
3 changed files with 0 additions and 181 deletions

1
go.mod
View File

@ -108,7 +108,6 @@ require (
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/tools v0.0.0-20200721032237-77f530d86f9a
google.golang.org/api v0.17.0
google.golang.org/grpc v1.27.1
gopkg.in/vmihailenco/msgpack.v2 v2.9.1 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71

View File

@ -1,54 +0,0 @@
package grpc
import (
"encoding/json"
"errors"
platform "github.com/influxdata/influxdb/v2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ToStatus converts a platform.Error to a gRPC status message.
func ToStatus(err *platform.Error) (*status.Status, error) {
if err == nil {
return status.New(codes.OK, ""), nil
}
c := codes.Unknown
switch err.Code {
case platform.EInternal:
c = codes.Internal
case platform.ENotFound, platform.EMethodNotAllowed:
c = codes.NotFound
case platform.EConflict, platform.EInvalid, platform.EEmptyValue: // not really sure how to express this as gRPC only has Invalid
c = codes.InvalidArgument
case platform.EUnavailable:
c = codes.Unavailable
}
buf, jerr := json.Marshal(err)
if jerr != nil {
return nil, jerr
}
return status.New(c, string(buf)), nil
}
// FromStatus converts a gRPC status message to a platform.Error.
func FromStatus(s *status.Status) *platform.Error {
if s == nil || s.Code() == codes.OK {
return nil
}
msg := s.Message()
var perr platform.Error
if err := json.Unmarshal([]byte(msg), &perr); err != nil {
return &platform.Error{
Err: errors.New(msg),
Code: platform.EInternal,
}
}
return &perr
}

View File

@ -1,126 +0,0 @@
package grpc
import (
"fmt"
"reflect"
"testing"
platform "github.com/influxdata/influxdb/v2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestToStatus(t *testing.T) {
tests := []struct {
name string
err *platform.Error
wantCode codes.Code
wantMessage string
wantErr bool
}{
{
name: "encode nil error",
wantCode: codes.OK,
},
{
name: "encode internal error",
err: &platform.Error{
Err: fmt.Errorf("error"),
Op: "kit/grpc",
Code: platform.EInternal,
Msg: "howdy",
},
wantCode: codes.Internal,
wantMessage: `{"code":"internal error","message":"howdy","op":"kit/grpc","error":"error"}`,
},
{
name: "encode not found error",
err: &platform.Error{
Err: fmt.Errorf("error"),
Op: "kit/grpc",
Code: platform.ENotFound,
Msg: "howdy",
},
wantCode: codes.NotFound,
wantMessage: `{"code":"not found","message":"howdy","op":"kit/grpc","error":"error"}`,
},
{
name: "encode invalid error",
err: &platform.Error{
Err: fmt.Errorf("error"),
Op: "kit/grpc",
Code: platform.EInvalid,
Msg: "howdy",
},
wantCode: codes.InvalidArgument,
wantMessage: `{"code":"invalid","message":"howdy","op":"kit/grpc","error":"error"}`,
},
{
name: "encode unavailable error",
err: &platform.Error{
Err: fmt.Errorf("error"),
Op: "kit/grpc",
Code: platform.EUnavailable,
Msg: "howdy",
},
wantCode: codes.Unavailable,
wantMessage: `{"code":"unavailable","message":"howdy","op":"kit/grpc","error":"error"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToStatus(tt.err)
if (err != nil) != tt.wantErr {
t.Errorf("ToStatus() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got.Code() != tt.wantCode {
t.Errorf("ToStatus().Code() = %v, want %v", got.Code(), tt.wantCode)
}
if got.Message() != tt.wantMessage {
t.Errorf("ToStatus().Message() = %v, want %v", got.Message(), tt.wantMessage)
}
})
}
}
func TestFromStatus(t *testing.T) {
tests := []struct {
name string
s *status.Status
want *platform.Error
}{
{
name: "nil status returns nil error",
},
{
name: "OK status returns nil error",
s: status.New(codes.OK, ""),
},
{
name: "status message that is not JSON is internal error",
s: status.New(codes.Internal, "bad"),
want: &platform.Error{
Err: fmt.Errorf("bad"),
Code: platform.EInternal,
},
},
{
name: "status message with embedded platform error",
s: status.New(codes.Internal, `{"code":"unavailable","message":"howdy","op":"kit/grpc","error":"error"}`),
want: &platform.Error{
Err: fmt.Errorf("error"),
Code: platform.EUnavailable,
Msg: "howdy",
Op: "kit/grpc",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FromStatus(tt.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("FromStatus() = %v, want %v", got, tt.want)
}
})
}
}