提交 b1567cdc authored 作者: mooncake9527's avatar mooncake9527

hotfix

上级 7f5f8a24
......@@ -19,6 +19,7 @@ const (
HeaderXTimestampKey = "Timestamp"
KeyReqBody = "reqBody"
KeyRspBody = "rspBody"
KeyRspCode = "rspCode"
KeyOApiReqBody = "oApiReqBody%s"
KeyOApiRspBody = "oApiRspBody%s"
......
......@@ -158,11 +158,13 @@ func respJSONWith200(c *gin.Context, code int, msg string, data ...interface{})
// Success return success
func Success(c *gin.Context, data ...interface{}) {
c.Set(ctxUtils.KeyRspCode, 1)
respJSONWith200(c, errcode.Success.Code(), errcode.Success.Msg(), data...)
}
// SuccessWithPage return success
func SuccessWithPage[T any](c *gin.Context, list []*T, total int64) {
c.Set(ctxUtils.KeyRspCode, 1)
if list == nil {
list = []*T{}
}
......@@ -171,6 +173,7 @@ func SuccessWithPage[T any](c *gin.Context, list []*T, total int64) {
// SuccessWithList return success
func SuccessWithList[T any](c *gin.Context, list []*T) {
c.Set(ctxUtils.KeyRspCode, 1)
if list == nil {
list = []*T{}
}
......@@ -179,6 +182,7 @@ func SuccessWithList[T any](c *gin.Context, list []*T) {
// ErrorE return error
func ErrorE(c *gin.Context, err *errcode.Error, data ...interface{}) {
c.Set(ctxUtils.KeyRspCode, 0)
respJSONWith200(c, err.Code(), err.Msg(), data...)
}
......@@ -190,6 +194,7 @@ func ErrorE(c *gin.Context, err *errcode.Error, data ...interface{}) {
// - msg: string,错误消息。
// - data: ...interface{},可选的附加数据。
func Error(c *gin.Context, err error) {
c.Set(ctxUtils.KeyRspCode, 0)
// 如果原始错误不为空,则记录错误日志。
msg := ""
if err != nil {
......
......@@ -259,37 +259,6 @@ func (req *Request) push(ctx context.Context) (*Response, error) {
return req.send(ctx, buf, buf)
}
func (req *Request) ReqString(ctx context.Context) string {
request := make(map[string]any)
request["method"] = req.request.Method
request["url"] = req.request.URL.String()
request["headers"] = req.request.Header
if req.request.Method == http.MethodPost || req.request.Method == http.MethodPut || req.request.Method == http.MethodPatch || req.request.Method == http.MethodDelete {
if req.request.Body != nil {
buf := bytes.Buffer{}
n, _ := buf.ReadFrom(req.request.Body)
request["size"] = buf.Len()
if n > 0 {
request["body"] = getRequestBody(&buf, 256)
}
}
}
return xjson.ToJsonString(request)
}
func getRequestBody(buf *bytes.Buffer, maxLen int) []byte {
l := buf.Len()
if l == 0 {
return []byte("")
} else if l < maxLen {
return buf.Bytes()
}
body := make([]byte, maxLen)
copy(body, buf.Bytes())
return append(body[:maxLen-len(contentMark)], contentMark...)
}
func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer) (*Response, error) {
req.request, req.err = http.NewRequest(req.method, req.url, body)
if req.err != nil {
......@@ -341,7 +310,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return xerror.New(err.Error())
return xerror.Wrap(err, "read response body")
}
bodyBuf := bytes.NewBuffer(body)
if c, _ := ctxUtils.GetGinCtx(ctx); c != nil {
......@@ -413,6 +382,27 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
return resp, resp.err
}
func copyResponse(resp *Response, response *http.Response) {
if resp == nil || response == nil {
return
}
resp.Status = response.Status
resp.StatusCode = response.StatusCode
for k, v := range response.Header {
resp.Header[k] = append(resp.Header[k], v...)
}
if response.Body != nil {
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
resp.err = xerror.Wrap(err, "read response body")
}
if len(body) > 0 {
resp.Body = io.NopCloser(bytes.NewBuffer(body))
}
}
}
// Response return response
func (req *Request) Response() (*Response, error) {
if req.err != nil {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论