Skip to content

Instantly share code, notes, and snippets.

@bobbytables
Last active November 26, 2015 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbytables/f18da91913cd1336912c to your computer and use it in GitHub Desktop.
Save bobbytables/f18da91913cd1336912c to your computer and use it in GitHub Desktop.
syntax = "proto3";
message Error {
string message = 1;
int32 error_code = 20;
}
message Application {
string id = 1;
string type = 2;
}
message CreateApplicationRequest {
string type = 1;
}
message CreateApplicationResponse {
oneof SuccessOrError {
Application application = 1;
Error error = 20;
};
}
service Server {
// Sends a greeting
rpc CreateApplication (CreateApplicationRequest) returns (CreateApplicationResponse) {}
}
syntax = "proto3";
message Application {
string id = 1;
string type = 2;
string error = 20
}
service Server {
rpc CreateApplication (Application) returns (Application) {}
}
@harlow
Copy link

harlow commented Nov 25, 2015

Didn't know about oneof thats pretty cool!

I think prefer style2.proto. Seems more similar to signature from most Go functions. The CreateApplication function returns an error (or nil for error):

Client:

req :=  &pb.Request{...}
res, err := pb.CreateApplication(ctx, req)

if err != nil {
  // error handling
  return
}

Service:

func (s *server) CreateApplication(ctx context.Context, req *pb.Request) (*pb.Result, error) {
  // ...   
  if someErrorCondition == true {
    return &pb.Result{}, errors.New("Some error message")
  }

  return &pb.Result{...}, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment