1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-02-03 13:11:42 +02:00

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

This commit is contained in:
Casper-Mars 2021-09-07 10:46:46 +08:00 committed by GitHub
parent cd35233d1d
commit 2a4d440680
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 8 deletions

View File

@ -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)
}

View File

@ -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()
}

View File

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