example: errors (#2534)

* Adds readme.

* Fills readme.

* Adds readme.

* Moves error examples.

* Adds port flag.

* Changes the flag for clients.

* Adds package comments.
This commit is contained in:
Can Guler
2018-12-21 16:55:49 -08:00
committed by GitHub
parent f286604fb2
commit 25de51fc02
5 changed files with 43 additions and 35 deletions

View File

@ -32,6 +32,8 @@ import (
"google.golang.org/grpc/status"
)
var addr = flag.String("addr", "localhost:50052", "the address to connect to")
func unaryCall(c pb.EchoClient, requestID int, message string, want codes.Code) {
// Creates a context with a one second deadline for the RPC.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
@ -68,11 +70,9 @@ func streamingCall(c pb.EchoClient, requestID int, message string, want codes.Co
}
func main() {
port := flag.Int("port", 50052, "port number")
flag.Parse()
target := fmt.Sprintf("localhost:%v", *port)
conn, err := grpc.Dial(target, grpc.WithInsecure())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -1 +1,25 @@
Please see [examples/rpc_errors](../../rpc_errors).
# Description
This example demonstrates the use of status details in grpc errors.
# Run the sample code
Run the server:
```sh
$ go run ./server/main.go
```
Then run the client in another terminal:
```sh
$ go run ./client/main.go
```
It should succeed and print the greeting it received from the server.
Then run the client again:
```sh
$ go run ./client/main.go
```
This time, it should fail by printing error status details that it received from the server.

View File

@ -16,10 +16,12 @@
*
*/
// Binary client is an example client.
package main
import (
"context"
"flag"
"log"
"os"
"time"
@ -30,9 +32,13 @@ import (
"google.golang.org/grpc/status"
)
var addr = flag.String("addr", "localhost:50052", "the address to connect to")
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -16,10 +16,12 @@
*
*/
// Binary server is an example server.
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
@ -32,9 +34,7 @@ import (
"google.golang.org/grpc/status"
)
const (
port = ":50051"
)
var port = flag.Int("port", 50052, "port number")
// server is used to implement helloworld.GreeterServer.
type server struct {
@ -67,11 +67,14 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
}
func main() {
log.Printf("server starting on port %s...", port)
lis, err := net.Listen("tcp", port)
flag.Parse()
address := fmt.Sprintf(":%v", *port)
lis, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{count: make(map[string]int)})
if err := s.Serve(lis); err != nil {

View File

@ -1,25 +0,0 @@
# Description
This example demonstrates the use of status details in grpc errors.
# Run the sample code
Run the server:
```sh
$ go run ./server/main.go
```
Then run the client in another terminal:
```sh
$ go run ./client/main.go
```
It should succeed and print the greeting it received from the server.
Then run the client again:
```sh
$ go run ./client/main.go
```
This time, it should fail by printing error status details that it received from the server.