feat: protoc-gen-go-errors add comment (#1961)

pull/1973/head
JeffreyBool 3 years ago committed by GitHub
parent 9098e9dd39
commit 245f55d3ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      cmd/protoc-gen-go-errors/errors.go
  2. 4
      cmd/protoc-gen-go-errors/template.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
}

@ -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 {

Loading…
Cancel
Save