Add example of handling a required argument

More from:

    https://github.com/twitchtv/twirp/wiki/Best-Practices
This commit is contained in:
Stephen McQuay 2018-01-20 09:01:46 -08:00
parent 9ccfd44724
commit 7c35411a0a
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
2 changed files with 6 additions and 2 deletions

4
hwt.go
View File

@ -4,11 +4,15 @@ import (
"context" "context"
fmt "fmt" fmt "fmt"
"github.com/twitchtv/twirp"
pb "mcquay.me/hwt/rpc/hwt" pb "mcquay.me/hwt/rpc/hwt"
) )
type Server struct{} type Server struct{}
func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, error) { func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, error) {
if req.Subject == "" {
return nil, twirp.RequiredArgumentError("subject")
}
return &pb.HelloResp{Text: fmt.Sprintf("echo: %v", req.Subject)}, nil return &pb.HelloResp{Text: fmt.Sprintf("echo: %v", req.Subject)}, nil
} }

View File

@ -9,9 +9,9 @@ service HelloWorld {
} }
message HelloReq { message HelloReq {
string subject = 1; string subject = 1; // required
} }
message HelloResp { message HelloResp {
string text = 1; string text = 1; // required
} }