-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
75 lines (63 loc) · 2.32 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package grpc
import (
"errors"
"fmt"
"testing"
"google.golang.org/grpc/codes"
pb "github.com/binchencoder/gateway-proto/frontend"
)
const (
expected = `rpc error: code = InvalidArgument desc = {"code":500000,"params":["a","b","c"]}`
)
func TestErrorConversion(t *testing.T) {
e := pb.Error{
Code: pb.ErrorCode_UNDEFINED,
Params: []string{"a", "b", "c"},
}
err := ToGrpcError(codes.InvalidArgument, &e)
if err.Error() != expected {
t.Errorf("expect frontend error '%s' but got '%s'", expected, err.Error())
}
code, ee := FromGrpcError(err)
if code != codes.InvalidArgument {
t.Errorf("expect gRPC code '%d' but got '%d'", codes.InvalidArgument, code)
}
if ee.Code != pb.ErrorCode_UNDEFINED {
t.Errorf("expect overall code '%d' but got '%d'", pb.ErrorCode_UNDEFINED, ee.Code)
}
if fmt.Sprintf("%s", ee.Params) != fmt.Sprintf("%s", e.Params) {
t.Errorf("expect params '%s' but got '%s'", e.Params, ee.Params)
}
err = errors.New("err")
code, ee = FromGrpcError(err)
if code != codes.Unknown && ee != nil {
t.Error("unexpected result when err is not grpc error and FromGrpcError(err) is called.")
}
}
// TestGrpcInternalServerError tests the GrpcInternalServerError function.
func TestGrpcInternalServerError(t *testing.T) {
msg := "Fake Internal Server error!"
grpcErr := ToGrpcInternalError(errors.New(msg))
code, pbErr := FromGrpcError(grpcErr)
if code != codes.Internal {
t.Errorf("TestGrpcInternalServerError(): expect code as %d. While it's: %d ", codes.Internal, code)
}
if len(pbErr.Details) == 0 || pbErr.Details[0].Code != pb.ErrorCode_SERVER_ERROR ||
len(pbErr.Details[0].Params) == 0 || pbErr.Details[0].Params[0] != msg {
t.Error("TestGrpcInternalServerError(): invalid error details. Detail: ", pbErr.Details)
}
grpcErr2 := ToGrpcInternalError(grpcErr)
if grpcErr.Error() != grpcErr2.Error() {
t.Error("TestGrpcInternalServerError(): ToGrpcInternalError should not wrap exiting grpc internal error again.")
}
e := &pb.Error{
Code: pb.ErrorCode_UNDEFINED,
Params: []string{"a", "b", "c"},
}
grpcErr3 := ToGrpcError(codes.NotFound, e)
grpcErr4 := ToGrpcInternalError(grpcErr3)
expteced := ToGrpcError(codes.Internal, e)
if grpcErr4.Error() != expteced.Error() {
t.Error("TestGrpcInternalServerError(): ToGrpcInternalError should convert Code from codes.NotFound to codes.Internal.")
}
}