-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (75 loc) · 2.25 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"fmt"
"reflect"
"os"
"time"
pb "github.com/fluffy-bunny/grpcdotnetgo-samples/contracts/simple/helloworld"
"github.com/fluffy-bunny/grpcdotnetgo-samples/simple/internal"
_ "github.com/fluffy-bunny/grpcdotnetgo/pkg/proto/error"
"github.com/gogo/googleapis/google/rpc"
"github.com/gogo/status"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)
const (
address = "localhost:5105"
defaultName = "world"
)
var version = "development"
func main() {
fmt.Println("Version:\t", version)
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatal().Err(err).Msg("did not connect")
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
c2 := pb.NewGreeter2Client(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{
Name: name,
Directive: pb.HelloDirectives_HELLO_DIRECTIVES_ERROR,
})
if err != nil {
log.Error().Err(err).Msg("could not greet")
st, ok := status.FromError(err)
if ok {
log.Error().Interface("statusError", st).Send()
st = status.Convert(err)
for _, detail := range st.Details() {
fmt.Println(reflect.TypeOf(detail))
log.Error().Interface("detail", detail).Send()
switch t := detail.(type) {
case *rpc.BadRequest:
fmt.Println("Oops! Your request was rejected by the server.")
for _, violation := range t.GetFieldViolations() {
fmt.Printf("The %q field was wrong:\n", violation.GetField())
fmt.Printf("\t%s\n", violation.GetDescription())
}
}
}
}
return
}
log.Printf("Greeting: %s", internal.PrettyJSON(r))
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Second)
defer cancel2()
r2, err := c2.SayHello(ctx2, &pb.HelloRequest{Name: name})
if err != nil {
log.Error().Err(err).Msg("could not greet")
st, ok := status.FromError(err)
if ok {
log.Error().Interface("statusError", st).Send()
}
}
log.Printf("Greeting: %s", internal.PrettyJSON(r2))
}