feat: return error message in the grpc server (#34)

This commit is contained in:
Rick 2023-04-08 20:34:31 +08:00 committed by GitHub
parent a941923526
commit a27b13aae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 23 deletions

View File

@ -105,6 +105,7 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
reply = &HelloReply{}
for _, testCase := range suite.Items { for _, testCase := range suite.Items {
simpleRunner := runner.NewSimpleTestCaseRunner() simpleRunner := runner.NewSimpleTestCaseRunner()
@ -116,14 +117,14 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
testCase.Request.API = fmt.Sprintf("%s%s", suite.API, testCase.Request.API) testCase.Request.API = fmt.Sprintf("%s%s", suite.API, testCase.Request.API)
} }
var output interface{} if output, testErr := simpleRunner.RunTestCase(&testCase, dataContext, ctx); testErr == nil {
if output, err = simpleRunner.RunTestCase(&testCase, dataContext, ctx); err == nil {
dataContext[testCase.Name] = output dataContext[testCase.Name] = output
} else { } else {
reply.Error = testErr.Error()
break break
} }
} }
reply = &HelloReply{Message: buf.String()} reply.Message = buf.String()
return return
} }

View File

@ -93,6 +93,7 @@ func (m *TestTask) GetEnv() map[string]string {
type HelloReply struct { type HelloReply struct {
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"` XXX_sizecache int32 `json:"-"`
@ -130,6 +131,13 @@ func (m *HelloReply) GetMessage() string {
return "" return ""
} }
func (m *HelloReply) GetError() string {
if m != nil {
return m.Error
}
return ""
}
type Empty struct { type Empty struct {
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
@ -173,24 +181,25 @@ func init() {
} }
var fileDescriptor_36fb7b77b8f76c98 = []byte{ var fileDescriptor_36fb7b77b8f76c98 = []byte{
// 301 bytes of a gzipped FileDescriptorProto // 311 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0x4b, 0x4b, 0xfb, 0x40, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0x4f, 0x4b, 0xf3, 0x40,
0x14, 0xc5, 0xff, 0x69, 0xfa, 0xfa, 0x5f, 0x11, 0xca, 0x45, 0x70, 0xec, 0xaa, 0x64, 0x21, 0x05, 0x10, 0xc6, 0xdf, 0x34, 0xfd, 0xf7, 0x8e, 0x08, 0x65, 0x10, 0x8c, 0x3d, 0x95, 0x9c, 0x0a, 0xda,
0x6d, 0x82, 0x15, 0x44, 0x5c, 0x0a, 0x45, 0x57, 0x2e, 0x42, 0x71, 0xe1, 0x6e, 0xda, 0x5e, 0xe2, 0x04, 0x2b, 0x88, 0x88, 0x27, 0xa1, 0xe8, 0xc9, 0x43, 0x28, 0x1e, 0xbc, 0x6d, 0xdb, 0x21, 0xc6,
0x98, 0xc9, 0x24, 0xcc, 0x23, 0x98, 0x8f, 0xe8, 0xb7, 0x92, 0xbc, 0x2c, 0x88, 0xab, 0x39, 0xe7, 0x6c, 0x36, 0x61, 0x77, 0x13, 0xcc, 0x47, 0xf4, 0x5b, 0xc9, 0x6e, 0xb2, 0x16, 0xc4, 0xd3, 0xce,
0x37, 0xf7, 0x71, 0xe0, 0xc2, 0x79, 0x91, 0x26, 0x91, 0x21, 0x5d, 0x92, 0xee, 0x9e, 0xb0, 0xd0, 0xef, 0xd9, 0x99, 0x79, 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, 0x5d, 0xb8, 0xcf, 0xb3, 0x48, 0x0a, 0xe5, 0x3e, 0x8d, 0xd3, 0xa4, 0x22, 0xd7, 0x63, 0xb7, 0xfe, 0x3d, 0xf2, 0x18, 0xbd, 0x5d, 0xa5, 0x99, 0x7e, 0xaf, 0x77, 0xd1, 0xbe,
0x5e, 0x88, 0x95, 0x25, 0x63, 0x85, 0x4a, 0xa2, 0xe3, 0xb5, 0x76, 0xe3, 0xe6, 0x4e, 0xb7, 0xdf, 0x2c, 0x62, 0x9e, 0x89, 0xfa, 0x53, 0xd5, 0x92, 0x44, 0xcc, 0xaa, 0x6c, 0xa5, 0x49, 0xe9, 0x4c,
0x01, 0x00, 0x00, 0xff, 0xff, 0xa2, 0x3b, 0xc8, 0x00, 0xc2, 0x01, 0x00, 0x00, 0xa4, 0xf1, 0xf1, 0x86, 0xbb, 0xb1, 0xbd, 0xde, 0xcd, 0x77, 0x00, 0x00, 0x00, 0xff, 0xff, 0x30,
0xde, 0x43, 0x18, 0xd8, 0x01, 0x00, 0x00,
} }

View File

@ -19,6 +19,7 @@ message TestTask {
message HelloReply { message HelloReply {
string message = 1; string message = 1;
string error = 2;
} }
message Empty { message Empty {

View File

@ -19,6 +19,7 @@ func TestUnimplement(t *testing.T) {
var reply *server.HelloReply var reply *server.HelloReply
assert.Empty(t, reply.GetMessage()) assert.Empty(t, reply.GetMessage())
assert.Empty(t, reply.GetError())
assert.Empty(t, &server.Empty{}) assert.Empty(t, &server.Empty{})
var task *server.TestTask var task *server.TestTask
@ -47,6 +48,7 @@ func TestServer(t *testing.T) {
reply, err := client.GetVersion(context.Background(), &server.Empty{}) reply, err := client.GetVersion(context.Background(), &server.Empty{})
assert.NotNil(t, reply) assert.NotNil(t, reply)
assert.Equal(t, "version", reply.GetMessage()) assert.Equal(t, "version", reply.GetMessage())
assert.Empty(t, reply.GetError())
assert.Nil(t, err) assert.Nil(t, err)
reply, err = client.Run(context.Background(), &server.TestTask{}) reply, err = client.Run(context.Background(), &server.TestTask{})