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
for _, enum := range file.Enums {
skip := genErrorsReason(gen, file, g, enum)
if skip == false {
if !skip {
index++
}
}
@ -70,7 +70,7 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
enumCode = int(ok)
}
// 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 {
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()),
Value: string(v.Desc.Name()),
CamelValue: Case2Camel(string(v.Desc.Name())),
HttpCode: enumCode,
HTTPCode: enumCode,
}
ew.Errors = append(ew.Errors, err)
}

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

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

Loading…
Cancel
Save