From 7c35411a0a24c97b1df4f72fd6271d10356ab17d Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sat, 20 Jan 2018 09:01:46 -0800 Subject: [PATCH] Add example of handling a required argument More from: https://github.com/twitchtv/twirp/wiki/Best-Practices --- hwt.go | 4 ++++ rpc/hwt/service.proto | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/hwt.go b/hwt.go index f71525f..95bba28 100644 --- a/hwt.go +++ b/hwt.go @@ -4,11 +4,15 @@ import ( "context" fmt "fmt" + "github.com/twitchtv/twirp" pb "mcquay.me/hwt/rpc/hwt" ) type Server struct{} 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 } diff --git a/rpc/hwt/service.proto b/rpc/hwt/service.proto index 9cf77fa..7313da1 100644 --- a/rpc/hwt/service.proto +++ b/rpc/hwt/service.proto @@ -9,9 +9,9 @@ service HelloWorld { } message HelloReq { - string subject = 1; + string subject = 1; // required } message HelloResp { - string text = 1; + string text = 1; // required }