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:
@ -32,6 +32,8 @@ import (
|
|||||||
"google.golang.org/grpc/status"
|
"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) {
|
func unaryCall(c pb.EchoClient, requestID int, message string, want codes.Code) {
|
||||||
// Creates a context with a one second deadline for the RPC.
|
// Creates a context with a one second deadline for the RPC.
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
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() {
|
func main() {
|
||||||
port := flag.Int("port", 50052, "port number")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
target := fmt.Sprintf("localhost:%v", *port)
|
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
|
||||||
conn, err := grpc.Dial(target, grpc.WithInsecure())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("did not connect: %v", err)
|
log.Fatalf("did not connect: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
|
@ -16,10 +16,12 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Binary client is an example client.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -30,9 +32,13 @@ import (
|
|||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var addr = flag.String("addr", "localhost:50052", "the address to connect to")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
// Set up a connection to the server.
|
// 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 {
|
if err != nil {
|
||||||
log.Fatalf("did not connect: %v", err)
|
log.Fatalf("did not connect: %v", err)
|
||||||
}
|
}
|
@ -16,10 +16,12 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Binary server is an example server.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -32,9 +34,7 @@ import (
|
|||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var port = flag.Int("port", 50052, "port number")
|
||||||
port = ":50051"
|
|
||||||
)
|
|
||||||
|
|
||||||
// server is used to implement helloworld.GreeterServer.
|
// server is used to implement helloworld.GreeterServer.
|
||||||
type server struct {
|
type server struct {
|
||||||
@ -67,11 +67,14 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Printf("server starting on port %s...", port)
|
flag.Parse()
|
||||||
lis, err := net.Listen("tcp", port)
|
|
||||||
|
address := fmt.Sprintf(":%v", *port)
|
||||||
|
lis, err := net.Listen("tcp", address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to listen: %v", err)
|
log.Fatalf("failed to listen: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s := grpc.NewServer()
|
s := grpc.NewServer()
|
||||||
pb.RegisterGreeterServer(s, &server{count: make(map[string]int)})
|
pb.RegisterGreeterServer(s, &server{count: make(map[string]int)})
|
||||||
if err := s.Serve(lis); err != nil {
|
if err := s.Serve(lis); err != nil {
|
@ -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.
|
|
Reference in New Issue
Block a user