Skip to content

Commit

Permalink
优化response
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Jan 15, 2024
1 parent 67dddd6 commit 38733d8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 36 deletions.
5 changes: 5 additions & 0 deletions exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func (e *APIException) WithData(d interface{}) *APIException {
return e
}

func (e *APIException) WithMessage(m string) *APIException {
e.Message = m
return e
}

func (e *APIException) GetData() interface{} {
return e.Data
}
Expand Down
47 changes: 47 additions & 0 deletions http/gin/response/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package response

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/exception"
"github.com/infraboard/mcube/v2/http/response"
"github.com/infraboard/mcube/v2/ioc/config/application"
)

// 正常请求数据返回
func Success(c *gin.Context, data any) {
// 是否需要脱敏
if v, ok := data.(response.DesenseObj); ok {
v.Desense()
}

c.JSON(http.StatusOK, data)
}

// 异常情况的数据返回, 返回我们的业务Exception
func Failed(c *gin.Context, err error) {
// 如果出现多个Handler, 需要通过手动abord
defer c.Abort()

var e *exception.APIException
if v, ok := err.(*exception.APIException); ok {
e = v
} else {
// 非可以预期, 没有定义业务的情况
e = exception.NewAPIException(
http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError),
"%s",
err.Error(),
)
e.HttpCode = http.StatusInternalServerError

}

if e.Namespace == "" {
e.WithNamespace(application.Get().AppName)
}

c.JSON(e.HttpCode, e)
}
File renamed without changes.
53 changes: 17 additions & 36 deletions http/restful/response/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,31 @@ import (
"github.com/emicklei/go-restful/v3"
"github.com/infraboard/mcube/v2/exception"
"github.com/infraboard/mcube/v2/http/response"
"github.com/infraboard/mcube/v2/ioc/config/application"
"github.com/infraboard/mcube/v2/ioc/config/log"
)

// Failed use to response error messge
func Failed(w *restful.Response, err error, opts ...response.Option) {
var (
errCode int
httpCode int
ns string
reason string
data interface{}
meta interface{}
)

switch t := err.(type) {
case *exception.APIException:
errCode = t.ErrorCode()
reason = t.GetReason()
data = t.GetData()
meta = t.GetMeta()
ns = t.GetNamespace()
httpCode = t.GetHttpCode()
default:
errCode = exception.UnKnownException
}

if httpCode == 0 {
httpCode = http.StatusInternalServerError
}

resp := response.Data{
Code: &errCode,
Namespace: ns,
Reason: reason,
Message: err.Error(),
Data: data,
Meta: meta,
var e *exception.APIException
if v, ok := err.(*exception.APIException); ok {
e = v
} else {
// 非可以预期, 没有定义业务的情况
e = exception.NewAPIException(
http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError),
"%s",
err.Error(),
)
e.HttpCode = http.StatusInternalServerError
}

for _, opt := range opts {
opt.Apply(&resp)
if e.Namespace == "" {
e.WithNamespace(application.Get().AppName)
}

err = w.WriteHeaderAndEntity(httpCode, resp)
err = w.WriteHeaderAndEntity(e.HttpCode, e)
if err != nil {
log.L().Error().Msgf("send failed response error, %s", err)
}
Expand All @@ -58,7 +39,7 @@ func Failed(w *restful.Response, err error, opts ...response.Option) {
// Success use to response success data
func Success(w *restful.Response, data any, opts ...response.Option) {
// 是否需要脱敏
if v, ok := data.(DesenseObj); ok {
if v, ok := data.(response.DesenseObj); ok {
v.Desense()
}

Expand Down
2 changes: 2 additions & 0 deletions ioc/config/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func init() {
ioc.Config().Registry(&Application{
AppName: "default",
Domain: "localhost",
EncryptKey: "defualt app encrypt key",
CipherPrefix: "@ciphered@",
})
Expand All @@ -19,6 +20,7 @@ type Application struct {

AppName string `json:"name" yaml:"name" toml:"name" env:"APP_NAME"`
AppDescription string `json:"description" yaml:"description" toml:"description" env:"APP_DESCRIPTION"`
Domain string `json:"domain" yaml:"domain" toml:"domain" env:"APP_DOMAIN"`
EncryptKey string `json:"encrypt_key" yaml:"encrypt_key" toml:"encrypt_key" env:"APP_ENCRYPT_KEY"`
CipherPrefix string `json:"cipher_prefix" yaml:"cipher_prefix" toml:"cipher_prefix" env:"APP_CIPHER_PREFIX"`
}
Expand Down

0 comments on commit 38733d8

Please sign in to comment.