Skip to content

Commit 5f30dd9

Browse files
committed
custom response in handler
1 parent 1db672c commit 5f30dd9

File tree

8 files changed

+41
-6
lines changed

8 files changed

+41
-6
lines changed

api/serverNameExample/v1/userExample.proto

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,18 @@ service userExample {
124124
}
125125
}
126126

127+
127128
// Some notes on defining fields under message:
128129
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
129-
// (2) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
130-
// you must add annotations for snake case names, such as string foo_bar = 1 [json_name = "foo_bar"],
130+
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
131+
// use xxxID naming format, such as userID, orderID, etc.
132+
// (3) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
133+
// you must add annotations for snake case names, such as string fieldName = 1 [json_name = "field_name"],
131134
// to ensure that the front end and back end JSON naming is consistent.
132-
// (3) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
135+
// (4) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
133136
// message must contain the name of the path parameter and the name should be
134137
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
135-
// (4) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
138+
// (5) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
136139
// a form tag must be added when defining the query parameter in the message,
137140
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
138141

api/serverNameExample/v1/userExample_router.pb.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/protoc-gen-go-gin/internal/generate/router/gen.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
const (
1515
stringsPkg = protogen.GoImportPath("strings")
1616
contextPkg = protogen.GoImportPath("context")
17+
errorsPkg = protogen.GoImportPath("errors")
1718
errcodePkg = protogen.GoImportPath("github.com/zhufuyi/sponge/pkg/errcode")
1819
middlewarePkg = protogen.GoImportPath("github.com/zhufuyi/sponge/pkg/gin/middleware")
1920
zapPkg = protogen.GoImportPath("go.uber.org/zap")
@@ -35,8 +36,8 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
3536
g.P("package ", file.GoPackageName)
3637
g.P()
3738

38-
g.P("// import packages: ", stringsPkg.Ident(" "), contextPkg.Ident(" "), errcodePkg.Ident(" "),
39-
middlewarePkg.Ident(" "), zapPkg.Ident(" "), ginPkg.Ident(" "))
39+
g.P("// import packages: ", stringsPkg.Ident(" "), contextPkg.Ident(" "), errorsPkg.Ident(" "),
40+
errcodePkg.Ident(" "), middlewarePkg.Ident(" "), zapPkg.Ident(" "), ginPkg.Ident(" "))
4041
g.P()
4142

4243
for _, s := range file.Services {

cmd/protoc-gen-go-gin/internal/generate/router/template.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ func (r *{{$.LowerName}}Router) withMiddleware(method string, path string, fn gi
197197
198198
out, err := r.iLogic.{{.Name}}(ctx, req)
199199
if err != nil {
200+
if errors.Is(err, errcode.SkipResponse) {
201+
return
202+
}
200203
r.iResponse.Error(c, err)
201204
return
202205
}

internal/ecode/systemCode_http.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ var (
3434
Unimplemented = errcode.Unimplemented
3535
DataLoss = errcode.DataLoss
3636
)
37+
38+
var SkipResponse = errcode.SkipResponse

internal/ecode/systemCode_rpc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ var (
3737
func Any(key string, val interface{}) errcode.Detail {
3838
return errcode.Any(key, val)
3939
}
40+
41+
// StatusSkipResponse is only use for grpc-gateway
42+
var StatusSkipResponse = errcode.SkipResponse

pkg/errcode/response.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package errcode
22

33
import (
4+
"errors"
45
"net/http"
56
"strconv"
67
"strings"
@@ -10,6 +11,9 @@ import (
1011
"google.golang.org/grpc/status"
1112
)
1213

14+
// SkipResponse skip response
15+
var SkipResponse = errors.New("skip response") //nolint
16+
1317
// Responser response interface
1418
type Responser interface {
1519
Success(ctx *gin.Context, data interface{})

pkg/errcode/response_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ func TestParseCodeAndMsgError(t *testing.T) {
172172
}
173173
code, msg := parseCodeAndMsg2(st.String())
174174
t.Log("strings: ", code, msg)
175+
176+
st, _ = status.FromError(SkipResponse)
177+
t.Log(st.Code(), st.Message())
175178
}
176179

177180
var mcReg = regexp.MustCompile(`code\s*=\s*(\d+),\s*msg\s*=\s*(.+)`)

0 commit comments

Comments
 (0)