first commit
This commit is contained in:
89
common/result/httpResult.go
Normal file
89
common/result/httpResult.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"znc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// http返回
|
||||
func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
|
||||
|
||||
if err == nil {
|
||||
httpx.WriteJson(w, http.StatusOK, Success(resp))
|
||||
} else {
|
||||
//错误返回
|
||||
errcode := xerr.SERVER_COMMON_ERROR
|
||||
errmsg := "服务器开小差啦,稍后再来试一试"
|
||||
|
||||
causeErr := errors.Cause(err) // err类型
|
||||
if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型
|
||||
//自定义CodeError
|
||||
errcode = e.GetErrCode()
|
||||
errmsg = e.GetErrMsg()
|
||||
} else {
|
||||
if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误
|
||||
grpcCode := uint32(gstatus.Code())
|
||||
if xerr.IsCodeErr(grpcCode) { //区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端
|
||||
errcode = grpcCode
|
||||
errmsg = gstatus.Message()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logx.WithContext(r.Context()).Errorf("【API-ERR】 : %+v ", err)
|
||||
|
||||
httpx.WriteJson(w, http.StatusOK, Error(errcode, errmsg))
|
||||
}
|
||||
}
|
||||
|
||||
// 授权的http方法
|
||||
func AuthHttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
|
||||
|
||||
if err == nil {
|
||||
//成功返回
|
||||
r := Success(resp)
|
||||
httpx.WriteJson(w, http.StatusOK, r)
|
||||
} else {
|
||||
//错误返回
|
||||
errcode := xerr.SERVER_COMMON_ERROR
|
||||
errmsg := "服务器开小差啦,稍后再来试一试"
|
||||
|
||||
causeErr := errors.Cause(err) // err类型
|
||||
if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型
|
||||
//自定义CodeError
|
||||
errcode = e.GetErrCode()
|
||||
errmsg = e.GetErrMsg()
|
||||
} else {
|
||||
if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误
|
||||
grpcCode := uint32(gstatus.Code())
|
||||
if xerr.IsCodeErr(grpcCode) { //区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端
|
||||
errcode = grpcCode
|
||||
errmsg = gstatus.Message()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logx.WithContext(r.Context()).Errorf("【GATEWAY-ERR】 : %+v ", err)
|
||||
|
||||
httpx.WriteJson(w, http.StatusUnauthorized, Error(errcode, errmsg))
|
||||
}
|
||||
}
|
||||
|
||||
// http 参数错误返回
|
||||
func ParamErrorResult(r *http.Request, w http.ResponseWriter, err error) {
|
||||
errMsg := fmt.Sprintf("%s,%s", xerr.MapErrMsg(xerr.REUQEST_PARAM_ERROR), err.Error())
|
||||
httpx.WriteJson(w, http.StatusOK, Error(xerr.REUQEST_PARAM_ERROR, errMsg))
|
||||
}
|
||||
|
||||
// http 参数校验失败返回
|
||||
func ParamValidateErrorResult(r *http.Request, w http.ResponseWriter, err error) {
|
||||
//errMsg := fmt.Sprintf("%s,%s", xerr.MapErrMsg(xerr.REUQEST_PARAM_ERROR), err.Error())
|
||||
httpx.WriteJson(w, http.StatusOK, Error(xerr.PARAM_VERIFICATION_ERROR, err.Error()))
|
||||
}
|
||||
44
common/result/jobResult.go
Normal file
44
common/result/jobResult.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"znc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// job返回
|
||||
func JobResult(ctx context.Context, resp interface{}, err error) {
|
||||
if err == nil {
|
||||
// 成功返回 ,只有dev环境下才会打印info,线上不显示
|
||||
if resp != nil {
|
||||
logx.Infof("resp: %+v", resp)
|
||||
}
|
||||
return
|
||||
} else {
|
||||
errCode := xerr.SERVER_COMMON_ERROR
|
||||
errMsg := "服务器开小差啦,稍后再来试一试"
|
||||
|
||||
// 错误返回
|
||||
causeErr := errors.Cause(err) // err类型
|
||||
if e, ok := causeErr.(*xerr.CodeError); ok { // 自定义错误类型
|
||||
// 自定义CodeError
|
||||
errCode = e.GetErrCode()
|
||||
errMsg = e.GetErrMsg()
|
||||
} else {
|
||||
if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误
|
||||
grpcCode := uint32(gstatus.Code())
|
||||
if xerr.IsCodeErr(grpcCode) { // 区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端
|
||||
errCode = grpcCode
|
||||
errMsg = gstatus.Message()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logx.WithContext(ctx).Errorf("【JOB-ERR】 : %+v ,errCode:%d , errMsg:%s ", err, errCode, errMsg)
|
||||
return
|
||||
}
|
||||
}
|
||||
21
common/result/responseBean.go
Normal file
21
common/result/responseBean.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package result
|
||||
|
||||
type ResponseSuccessBean struct {
|
||||
Code uint32 `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
type NullJson struct{}
|
||||
|
||||
func Success(data interface{}) *ResponseSuccessBean {
|
||||
return &ResponseSuccessBean{200, "OK", data}
|
||||
}
|
||||
|
||||
type ResponseErrorBean struct {
|
||||
Code uint32 `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func Error(errCode uint32, errMsg string) *ResponseErrorBean {
|
||||
return &ResponseErrorBean{errCode, errMsg}
|
||||
}
|
||||
Reference in New Issue
Block a user