added printFormattedStatus to main package
This commit is contained in:
parent
71db189b99
commit
8243c87e86
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"google.golang.org/grpc/keepalive"
|
"google.golang.org/grpc/keepalive"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
|
reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
// Register gzip compressor so compressed responses will work:
|
// Register gzip compressor so compressed responses will work:
|
||||||
_ "google.golang.org/grpc/encoding/gzip"
|
_ "google.golang.org/grpc/encoding/gzip"
|
||||||
|
|
@ -414,6 +415,13 @@ func main() {
|
||||||
}
|
}
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
printFormattedStatus := func(w io.Writer, stat *status.Status, formatter grpcurl.Formatter) {
|
||||||
|
formattedStatus, err := formatter(stat.Proto())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "ERROR: %v", err.Error())
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, formattedStatus)
|
||||||
|
}
|
||||||
|
|
||||||
if *expandHeaders {
|
if *expandHeaders {
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -656,7 +664,7 @@ func main() {
|
||||||
}
|
}
|
||||||
if h.Status.Code() != codes.OK {
|
if h.Status.Code() != codes.OK {
|
||||||
if *formatError {
|
if *formatError {
|
||||||
grpcurl.PrintFormattedStatus(os.Stderr, h.Status, formatter)
|
printFormattedStatus(os.Stderr, h.Status, formatter)
|
||||||
} else {
|
} else {
|
||||||
grpcurl.PrintStatus(os.Stderr, h.Status, formatter)
|
grpcurl.PrintStatus(os.Stderr, h.Status, formatter)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
format.go
10
format.go
|
|
@ -467,13 +467,3 @@ func PrintStatus(w io.Writer, stat *status.Status, formatter Formatter) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintFormattedStatus writes the gRPC status response in its entirety given the
|
|
||||||
// provided Formatter
|
|
||||||
func PrintFormattedStatus(w io.Writer, stat *status.Status, formatter Formatter) {
|
|
||||||
formattedStatus, err := formatter(stat.Proto())
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "ERROR: %v", err.Error())
|
|
||||||
}
|
|
||||||
fmt.Fprint(w, formattedStatus)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,7 @@ import (
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/golang/protobuf/ptypes/struct"
|
"github.com/golang/protobuf/ptypes/struct"
|
||||||
"github.com/jhump/protoreflect/desc"
|
"github.com/jhump/protoreflect/desc"
|
||||||
"google.golang.org/grpc/codes"
|
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
"google.golang.org/grpc/status"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRequestParser(t *testing.T) {
|
func TestRequestParser(t *testing.T) {
|
||||||
|
|
@ -175,38 +173,6 @@ func TestHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrintFormattedStatus(t *testing.T) {
|
|
||||||
jsonFormatter := NewJSONFormatter(
|
|
||||||
true,
|
|
||||||
anyResolverWithFallback{AnyResolver: jsonpb.Marshaler{Indent: " "}.AnyResolver})
|
|
||||||
textFormatter := NewTextFormatter(true)
|
|
||||||
testCases := []struct {
|
|
||||||
input *status.Status
|
|
||||||
formatter Formatter
|
|
||||||
expectedOutput string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
input: status.New(codes.InvalidArgument, "Missing Argument"),
|
|
||||||
formatter: jsonFormatter,
|
|
||||||
expectedOutput: statusAsJSON,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: status.New(codes.InvalidArgument, "Missing Argument"),
|
|
||||||
formatter: textFormatter,
|
|
||||||
expectedOutput: statusAsText,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range testCases {
|
|
||||||
var b bytes.Buffer
|
|
||||||
PrintFormattedStatus(&b, tc.input, tc.formatter)
|
|
||||||
got := b.String()
|
|
||||||
if !compare(tc.expectedOutput, got) {
|
|
||||||
t.Errorf("Incorrect output. Expected:\n%s\nGot:\n%s", tc.expectedOutput, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// compare checks that actual and expected are equal, returning true if so.
|
// compare checks that actual and expected are equal, returning true if so.
|
||||||
// A simple equality check (==) does not suffice because jsonpb formats
|
// A simple equality check (==) does not suffice because jsonpb formats
|
||||||
// structpb.Value strangely. So if that formatting gets fixed, we don't
|
// structpb.Value strangely. So if that formatting gets fixed, we don't
|
||||||
|
|
@ -278,14 +244,6 @@ Response contents:
|
||||||
"null": null
|
"null": null
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
statusAsJSON = `{
|
|
||||||
"code": 3,
|
|
||||||
"message": "Missing Argument",
|
|
||||||
"details": [
|
|
||||||
]
|
|
||||||
}`
|
|
||||||
statusAsText = `code: 3
|
|
||||||
message: "Missing Argument"`
|
|
||||||
messageAsText = `struct_value: <
|
messageAsText = `struct_value: <
|
||||||
fields: <
|
fields: <
|
||||||
key: "bar"
|
key: "bar"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue