Skip to content

Commit

Permalink
Merge pull request #27 from gengo/feature/report-parse-error
Browse files Browse the repository at this point in the history
Report semantic errors in the source to protoc
  • Loading branch information
yugui committed Jul 1, 2015
2 parents 8ac6df9 + 2da30c9 commit 5cbe2de
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import (
// It must be called after loadFile is called for all files so that loadServices
// can resolve names of message types and their fields.
func (r *Registry) loadServices(targetFile string) error {
glog.V(1).Infof("Loading services from %s", targetFile)
file := r.files[targetFile]
if file == nil {
return fmt.Errorf("no such file: %s", targetFile)
}
var svcs []*Service
for _, sd := range file.GetService() {
glog.V(2).Infof("Registering %s", sd.GetName())
svc := &Service{
File: file,
ServiceDescriptorProto: sd,
}
for _, md := range sd.GetMethod() {
glog.V(2).Infof("Processing %s.%s", sd.GetName(), md.GetName())
opts, err := extractAPIOptions(md)
if err != nil {
glog.Errorf("Failed to extract ApiMethodOptions from %s.%s: %v", svc.GetName(), md.GetName(), err)
Expand All @@ -44,6 +47,7 @@ func (r *Registry) loadServices(targetFile string) error {
if len(svc.Methods) == 0 {
continue
}
glog.V(2).Infof("Registered %s with %d method(s)", svc.GetName(), len(svc.Methods))
svcs = append(svcs, svc)
}
file.Services = svcs
Expand Down
2 changes: 2 additions & 0 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gengo/grpc-gateway/internal"
"github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
"github.com/golang/glog"
)

type param struct {
Expand Down Expand Up @@ -58,6 +59,7 @@ func applyTemplate(p param) (string, error) {
var methodSeen bool
for _, svc := range p.Services {
for _, meth := range svc.Methods {
glog.V(2).Infof("Processing %s.%s", svc.GetName(), meth.GetName())
methodSeen = true
for _, b := range meth.Bindings {
if err := handlerTemplate.Execute(w, binding{Binding: b}); err != nil {
Expand Down
26 changes: 19 additions & 7 deletions protoc-gen-grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func main() {
}

reg.SetPrefix(*importPrefix)
reg.Load(req)
if err := reg.Load(req); err != nil {
emitError(err)
return
}

g := gengateway.New(reg)

Expand All @@ -79,16 +82,25 @@ func main() {
targets = append(targets, f)
}

var resp plugin.CodeGeneratorResponse
out, err := g.Generate(targets)
glog.V(1).Info("Processed code generator request")
if err != nil {
resp.Error = proto.String(err.Error())
} else {
resp.File = out
emitError(err)
return
}
glog.V(1).Info("Processed code generator request")
emitFiles(out)
}

func emitFiles(out []*plugin.CodeGeneratorResponse_File) {
emitResp(&plugin.CodeGeneratorResponse{File: out})
}

func emitError(err error) {
emitResp(&plugin.CodeGeneratorResponse{Error: proto.String(err.Error())})
}

buf, err := proto.Marshal(&resp)
func emitResp(resp *plugin.CodeGeneratorResponse) {
buf, err := proto.Marshal(resp)
if err != nil {
glog.Fatal(err)
}
Expand Down

0 comments on commit 5cbe2de

Please sign in to comment.