diff --git a/cmd/protoc-gen-go-errors/errors.go b/cmd/protoc-gen-go-errors/errors.go index 7c2008d6f..9e8e02ad1 100644 --- a/cmd/protoc-gen-go-errors/errors.go +++ b/cmd/protoc-gen-go-errors/errors.go @@ -82,11 +82,19 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene if enumCode == 0 { continue } + + comment := v.Comments.Leading.String() + if comment == "" { + comment = v.Comments.Trailing.String() + } + err := &errorInfo{ Name: string(enum.Desc.Name()), Value: string(v.Desc.Name()), CamelValue: case2Camel(string(v.Desc.Name())), HTTPCode: enumCode, + Comment: comment, + HasComment: len(comment) > 0, } ew.Errors = append(ew.Errors, err) } @@ -94,6 +102,7 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene return true } g.P(ew.execute()) + return false } diff --git a/cmd/protoc-gen-go-errors/template.go b/cmd/protoc-gen-go-errors/template.go index 3b6e0fc32..e879fcc43 100644 --- a/cmd/protoc-gen-go-errors/template.go +++ b/cmd/protoc-gen-go-errors/template.go @@ -8,6 +8,7 @@ import ( var errorsTemplate = ` {{ range .Errors }} +{{if .HasComment}}{{.Comment}}{{end}} func Is{{.CamelValue}}(err error) bool { if err == nil { return false @@ -16,6 +17,7 @@ func Is{{.CamelValue}}(err error) bool { return e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HTTPCode}} } +{{if .HasComment}}{{.Comment}}{{end}} func Error{{.CamelValue}}(format string, args ...interface{}) *errors.Error { return errors.New({{.HTTPCode}}, {{.Name}}_{{.Value}}.String(), fmt.Sprintf(format, args...)) } @@ -28,6 +30,8 @@ type errorInfo struct { Value string HTTPCode int CamelValue string + Comment string + HasComment bool } type errorWrapper struct {