Skip to content

Commit 25de51f

Browse files
author
Can Guler
authored
example: errors (#2534)
* Adds readme. * Fills readme. * Adds readme. * Moves error examples. * Adds port flag. * Changes the flag for clients. * Adds package comments.
1 parent f286604 commit 25de51f

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed

Diff for: examples/features/deadline/client/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"google.golang.org/grpc/status"
3333
)
3434

35+
var addr = flag.String("addr", "localhost:50052", "the address to connect to")
36+
3537
func unaryCall(c pb.EchoClient, requestID int, message string, want codes.Code) {
3638
// Creates a context with a one second deadline for the RPC.
3739
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
@@ -68,11 +70,9 @@ func streamingCall(c pb.EchoClient, requestID int, message string, want codes.Co
6870
}
6971

7072
func main() {
71-
port := flag.Int("port", 50052, "port number")
7273
flag.Parse()
7374

74-
target := fmt.Sprintf("localhost:%v", *port)
75-
conn, err := grpc.Dial(target, grpc.WithInsecure())
75+
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
7676
if err != nil {
7777
log.Fatalf("did not connect: %v", err)
7878
}

Diff for: examples/features/errors/README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
Please see [examples/rpc_errors](../../rpc_errors).
1+
# Description
2+
3+
This example demonstrates the use of status details in grpc errors.
4+
5+
# Run the sample code
6+
7+
Run the server:
8+
9+
```sh
10+
$ go run ./server/main.go
11+
```
12+
Then run the client in another terminal:
13+
14+
```sh
15+
$ go run ./client/main.go
16+
```
17+
18+
It should succeed and print the greeting it received from the server.
19+
Then run the client again:
20+
21+
```sh
22+
$ go run ./client/main.go
23+
```
24+
25+
This time, it should fail by printing error status details that it received from the server.

Diff for: examples/rpc_errors/client/main.go renamed to examples/features/errors/client/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*
1717
*/
1818

19+
// Binary client is an example client.
1920
package main
2021

2122
import (
2223
"context"
24+
"flag"
2325
"log"
2426
"os"
2527
"time"
@@ -30,9 +32,13 @@ import (
3032
"google.golang.org/grpc/status"
3133
)
3234

35+
var addr = flag.String("addr", "localhost:50052", "the address to connect to")
36+
3337
func main() {
38+
flag.Parse()
39+
3440
// Set up a connection to the server.
35-
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
41+
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
3642
if err != nil {
3743
log.Fatalf("did not connect: %v", err)
3844
}

Diff for: examples/rpc_errors/server/main.go renamed to examples/features/errors/server/main.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*
1717
*/
1818

19+
// Binary server is an example server.
1920
package main
2021

2122
import (
2223
"context"
24+
"flag"
2325
"fmt"
2426
"log"
2527
"net"
@@ -32,9 +34,7 @@ import (
3234
"google.golang.org/grpc/status"
3335
)
3436

35-
const (
36-
port = ":50051"
37-
)
37+
var port = flag.Int("port", 50052, "port number")
3838

3939
// server is used to implement helloworld.GreeterServer.
4040
type server struct {
@@ -67,11 +67,14 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
6767
}
6868

6969
func main() {
70-
log.Printf("server starting on port %s...", port)
71-
lis, err := net.Listen("tcp", port)
70+
flag.Parse()
71+
72+
address := fmt.Sprintf(":%v", *port)
73+
lis, err := net.Listen("tcp", address)
7274
if err != nil {
7375
log.Fatalf("failed to listen: %v", err)
7476
}
77+
7578
s := grpc.NewServer()
7679
pb.RegisterGreeterServer(s, &server{count: make(map[string]int)})
7780
if err := s.Serve(lis); err != nil {

Diff for: examples/rpc_errors/README.md

-25
This file was deleted.

0 commit comments

Comments
 (0)