You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool-biz/zero/statusz/handler.go

39 lines
1.1 KiB
Go

1 year ago
package statusz
import (
"git.noahlan.cn/noahlan/ntool-biz/core/i18n"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
)
// ResponseHandler API执行结果处理统一返回值类型与结构
func ResponseHandler(req *http.Request, w http.ResponseWriter, trans bool, resp any, err error) {
if err == nil {
r := nstatus.NewResultWithData(code.StatusOK, msg.Success, resp)
if trans {
r.Msg = i18n.Trans(req.Context(), r.Msg)
}
httpx.WriteJson(w, code.StatusOK, r)
return
}
result := nstatus.ConvertErr(err)
if result == nil {
// 不可能发生此情况,这里处理是以防万一
result = nstatus.NewResult(code.StatusFailed, msg.Failed)
}
result.Data = resp
if trans {
result.Msg = i18n.Trans(req.Context(), result.Msg)
}
c := result.Code
// 判断code如果不是http-status全部使用500
if result.Code > http.StatusNetworkAuthenticationRequired {
c = http.StatusInternalServerError
}
httpx.WriteJson(w, c, result)
}