|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package result
-
- import (
- "context"
- "fmt"
- "github.com/pkg/errors"
- "github.com/zeromicro/go-zero/core/logx"
- "github.com/zeromicro/go-zero/rest/httpx"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/helper/xerr"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
- "go.opentelemetry.io/otel/trace"
- "google.golang.org/grpc/status"
- "net/http"
- )
-
- type Body struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data interface{} `json:"data,omitempty"`
- TraceID string `json:"traceId,omitempty"`
- }
-
- // http返回
- func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
- if err == nil {
- //成功返回
- body := Body{}
- utils.Convert(resp, &body)
- if body.Msg == "" && body.Code == 0 {
- body.Code = 200
- body.Msg = "success"
- body.TraceID = traceIDFromContext(r.Context())
- body.Data = resp
- httpx.OkJson(w, body)
- return
- } else {
- body.TraceID = traceIDFromContext(r.Context())
- httpx.OkJson(w, body)
- }
- } 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()
- }
- } else { //返回原始错误
- errmsg = err.Error()
- }
-
- logx.WithContext(r.Context()).Errorf("【API-ERR】 : %+v ", err)
-
- httpx.WriteJson(w, http.StatusBadRequest, Error(errcode, errmsg, r.Context()))
- }
- }
-
- // 授权的http方法
- func AuthHttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
-
- if err == nil {
- //成功返回
- r := Success(resp, r.Context())
- 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, r.Context()))
- }
- }
-
- // 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.StatusBadRequest, Error(xerr.REUQEST_PARAM_ERROR, errMsg, r.Context()))
- }
-
- func traceIDFromContext(ctx context.Context) string {
- spanCtx := trace.SpanContextFromContext(ctx)
- if spanCtx.HasTraceID() {
- return spanCtx.TraceID().String()
- }
-
- return ""
- }
|