feat: 引入签名认证、加密工具包及大量goctl代码生成模板,并更新API、Admin和Node服务逻辑。
This commit is contained in:
@@ -3,7 +3,10 @@ package result
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zero-ppanel/zero-ppanel/pkg/xerr"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
@@ -23,3 +26,44 @@ func Error(code int, msg string) *Response {
|
||||
func OkJson(w http.ResponseWriter, data interface{}) {
|
||||
httpx.OkJson(w, Success(data))
|
||||
}
|
||||
|
||||
func Parse(r *http.Request, v interface{}) error {
|
||||
return httpx.Parse(r, v)
|
||||
}
|
||||
|
||||
// HttpResult global HTTP error handler
|
||||
func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
|
||||
if err == nil {
|
||||
httpx.WriteJson(w, http.StatusOK, Success(resp))
|
||||
return
|
||||
}
|
||||
|
||||
code, res := ErrHandler(err)
|
||||
logx.WithContext(r.Context()).Errorf("【API-ERR】 : %+v ", err)
|
||||
httpx.WriteJson(w, code, res)
|
||||
}
|
||||
|
||||
// ErrHandler 按照 go-zero 开发规范,处理全局错误
|
||||
func ErrHandler(err error) (int, any) {
|
||||
errCode := xerr.ServerError
|
||||
errMsg := xerr.MapErrMsg(errCode)
|
||||
|
||||
// 直接从错误中提取自定义代码和消息
|
||||
// 不进行过度包装,直接判定是否满足业务错误特征
|
||||
if e, ok := err.(interface{
|
||||
GetErrCode() int
|
||||
GetErrMsg() string
|
||||
}); ok {
|
||||
errCode = e.GetErrCode()
|
||||
errMsg = e.GetErrMsg()
|
||||
} else if gstatus, ok := status.FromError(err); ok {
|
||||
// 识别 gRPC 错误
|
||||
grpcCode := uint32(gstatus.Code())
|
||||
if xerr.IsCodeErr(grpcCode) {
|
||||
errCode = int(grpcCode)
|
||||
errMsg = gstatus.Message()
|
||||
}
|
||||
}
|
||||
|
||||
return http.StatusOK, Error(errCode, errMsg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user