From 1058f6c1ee860bd062dc44a08b1c647cf1f093e1 Mon Sep 17 00:00:00 2001 From: chanoe <90238995+chanoe@users.noreply.github.com> Date: Wed, 22 Dec 2021 12:15:50 +0800 Subject: [PATCH] fix(api): response data.code => http.status (#17) * fix:response data.code => http status --- .gitignore | 1 + apiserver/response/response.go | 8 ++++---- errcode/err_code.go | 2 +- go.mod | 2 ++ go.sum | 5 +++++ 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index c91eeb9..167d7e5 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,5 @@ google golint ./auth.yaml +exam diff --git a/apiserver/response/response.go b/apiserver/response/response.go index d9f4877..acca4da 100644 --- a/apiserver/response/response.go +++ b/apiserver/response/response.go @@ -31,7 +31,7 @@ type responseData struct { func SrvErrWithRest(resp *restful.Response, srvErr *errcode2.SrvError, data interface{}) { status := errcode2.CodeToStatus(srvErr.Code()) resp.WriteHeaderAndEntity(status, responseData{ - Code: srvErr.Code(), + Code: status, Msg: srvErr.Msg(), Data: data, }) @@ -39,7 +39,7 @@ func SrvErrWithRest(resp *restful.Response, srvErr *errcode2.SrvError, data inte func DefineWithRest(resp *restful.Response, status, code int, msg string, data interface{}) { resp.WriteHeaderAndEntity(status, responseData{ - Code: code, + Code: status, Msg: msg, Data: data, }) @@ -49,7 +49,7 @@ func SrvErrWithWriter(w http.ResponseWriter, serErr *errcode2.SrvError, data int status := errcode2.CodeToStatus(serErr.Code()) w.WriteHeader(status) resp := responseData{ - Code: serErr.Code(), + Code: status, Msg: serErr.Msg(), Data: data, } @@ -61,7 +61,7 @@ func SrvErrWithWriter(w http.ResponseWriter, serErr *errcode2.SrvError, data int func DefineWithWriter(w http.ResponseWriter, status, code int, msg string, data interface{}) { w.WriteHeader(status) resp := responseData{ - Code: code, + Code: status, Msg: msg, Data: data, } diff --git a/errcode/err_code.go b/errcode/err_code.go index 383fe99..8370ffd 100644 --- a/errcode/err_code.go +++ b/errcode/err_code.go @@ -20,7 +20,7 @@ import "net/http" // the HTTP status code 0~700 is compatible. const ( // CodeSuccess. - CodeSuccess = 0 + CodeSuccess = 200 // CodeUnexpectedError. CodeUnexpectedError = iota + 10000 CodeInvalidAccessToken diff --git a/go.mod b/go.mod index d604039..ef61c7b 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/go-sql-driver/mysql v1.6.0 github.com/go-stack/stack v1.8.1 // indirect github.com/golang-jwt/jwt v3.2.1+incompatible + github.com/google/gnostic v0.5.7 // indirect github.com/google/uuid v1.3.0 github.com/json-iterator/go v1.1.12 // indirect github.com/lib/pq v1.10.2 // indirect @@ -36,6 +37,7 @@ require ( golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 golang.org/x/net v0.0.0-20211109214657-ef0fda0de508 // indirect golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 + google.golang.org/protobuf v1.27.1 // indirect gorm.io/driver/mysql v1.1.3 gorm.io/gorm v1.22.2 ) diff --git a/go.sum b/go.sum index 6e7a7e9..218fa80 100644 --- a/go.sum +++ b/go.sum @@ -583,6 +583,8 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/cel-go v0.7.3/go.mod h1:4EtyFAHT5xNr0Msu0MJjyGxPUgdr9DlcaPyzLt/kkt8= github.com/google/cel-spec v0.5.0/go.mod h1:Nwjgxy5CbjlPrtCWjeDjUyKMl8w41YBYGjsyDdqk0xA= +github.com/google/gnostic v0.5.7 h1:0x05jSyQvEQj2s7X6zg9Mdk1Gf9vI2DzSBJbMIVdiMU= +github.com/google/gnostic v0.5.7/go.mod h1:73MKFl6jIHelAJNaBGFzt3SPtZULs9dYrGFt8OiIsHQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -1715,6 +1717,7 @@ google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6D google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20201102152239-715cce707fb0/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= @@ -1769,6 +1772,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=