fix(cmd/protoc-gen-go-errors):part of #1430,fix cmd/protoc-gen-go-errors (#1435)

pull/1438/head
Casper-Mars 3 years ago committed by GitHub
parent cd35233d1d
commit 2a4d440680
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      cmd/protoc-gen-go-errors/errors.go
  2. 9
      cmd/protoc-gen-go-errors/template.go
  3. 1
      hack/.lintcheck_failures

@ -43,7 +43,7 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
index := 0 index := 0
for _, enum := range file.Enums { for _, enum := range file.Enums {
skip := genErrorsReason(gen, file, g, enum) skip := genErrorsReason(gen, file, g, enum)
if skip == false { if !skip {
index++ index++
} }
} }
@ -70,7 +70,7 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
enumCode = int(ok) enumCode = int(ok)
} }
// If the current enumeration does not contain 'errors.code' // If the current enumeration does not contain 'errors.code'
//or the code value exceeds the range, the current enum will be skipped // or the code value exceeds the range, the current enum will be skipped
if enumCode > 600 || enumCode < 0 { if enumCode > 600 || enumCode < 0 {
panic(fmt.Sprintf("Enum '%s' range must be greater than 0 and less than or equal to 600", string(v.Desc.Name()))) panic(fmt.Sprintf("Enum '%s' range must be greater than 0 and less than or equal to 600", string(v.Desc.Name())))
} }
@ -81,7 +81,7 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
Name: string(enum.Desc.Name()), Name: string(enum.Desc.Name()),
Value: string(v.Desc.Name()), Value: string(v.Desc.Name()),
CamelValue: Case2Camel(string(v.Desc.Name())), CamelValue: Case2Camel(string(v.Desc.Name())),
HttpCode: enumCode, HTTPCode: enumCode,
} }
ew.Errors = append(ew.Errors, err) ew.Errors = append(ew.Errors, err)
} }

@ -10,11 +10,11 @@ var errorsTemplate = `
func Is{{.CamelValue}}(err error) bool { func Is{{.CamelValue}}(err error) bool {
e := errors.FromError(err) e := errors.FromError(err)
return e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HttpCode}} return e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HTTPCode}}
} }
func Error{{.CamelValue}}(format string, args ...interface{}) *errors.Error { func Error{{.CamelValue}}(format string, args ...interface{}) *errors.Error {
return errors.New({{.HttpCode}}, {{.Name}}_{{.Value}}.String(), fmt.Sprintf(format, args...)) return errors.New({{.HTTPCode}}, {{.Name}}_{{.Value}}.String(), fmt.Sprintf(format, args...))
} }
{{- end }} {{- end }}
@ -23,9 +23,10 @@ func Error{{.CamelValue}}(format string, args ...interface{}) *errors.Error {
type errorInfo struct { type errorInfo struct {
Name string Name string
Value string Value string
HttpCode int HTTPCode int
CamelValue string CamelValue string
} }
type errorWrapper struct { type errorWrapper struct {
Errors []*errorInfo Errors []*errorInfo
} }
@ -39,5 +40,5 @@ func (e *errorWrapper) execute() string {
if err := tmpl.Execute(buf, e); err != nil { if err := tmpl.Execute(buf, e); err != nil {
panic(err) panic(err)
} }
return string(buf.Bytes()) return buf.String()
} }

@ -1,5 +1,4 @@
./examples ./examples
./cmd/protoc-gen-go-errors
./cmd/protoc-gen-go-http ./cmd/protoc-gen-go-http
./cmd/kratos ./cmd/kratos
./contrib/metrics/datadog ./contrib/metrics/datadog

Loading…
Cancel
Save