You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

httpResult.go 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package result
  13. import (
  14. "context"
  15. "fmt"
  16. "github.com/pkg/errors"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. "github.com/zeromicro/go-zero/rest/httpx"
  19. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/helper/xerr"
  20. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  21. "go.opentelemetry.io/otel/trace"
  22. "google.golang.org/grpc/status"
  23. "net/http"
  24. )
  25. type Body struct {
  26. Code int `json:"code"`
  27. Msg string `json:"msg"`
  28. Data interface{} `json:"data,omitempty"`
  29. TraceID string `json:"traceId,omitempty"`
  30. }
  31. // http返回
  32. func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
  33. if err == nil {
  34. //成功返回
  35. body := Body{}
  36. utils.Convert(resp, &body)
  37. if body.Msg == "" && body.Code == 0 {
  38. body.Code = 200
  39. body.Msg = "success"
  40. body.TraceID = traceIDFromContext(r.Context())
  41. body.Data = resp
  42. httpx.OkJson(w, body)
  43. return
  44. } else if body.Msg == "success" {
  45. body.Code = 200
  46. body.TraceID = traceIDFromContext(r.Context())
  47. httpx.OkJson(w, body)
  48. } else {
  49. body.TraceID = traceIDFromContext(r.Context())
  50. httpx.OkJson(w, body)
  51. }
  52. } else {
  53. //错误返回
  54. errcode := xerr.SERVER_COMMON_ERROR
  55. errmsg := "服务器开小差啦,稍后再来试一试"
  56. causeErr := errors.Cause(err) // err类型
  57. if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型
  58. //自定义CodeError
  59. errcode = e.GetErrCode()
  60. errmsg = e.GetErrMsg()
  61. } else if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误
  62. grpcCode := uint32(gstatus.Code())
  63. if xerr.IsCodeErr(grpcCode) { //区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端
  64. errcode = grpcCode
  65. errmsg = gstatus.Message()
  66. } else {
  67. errmsg = err.Error()
  68. }
  69. } else { //返回原始错误
  70. errmsg = err.Error()
  71. }
  72. logx.WithContext(r.Context()).Errorf("【API-ERR】 : %+v ", err)
  73. httpx.WriteJson(w, http.StatusBadRequest, Error(errcode, errmsg, r.Context()))
  74. }
  75. }
  76. // 授权的http方法
  77. func AuthHttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
  78. if err == nil {
  79. //成功返回
  80. r := Success(resp, r.Context())
  81. httpx.WriteJson(w, http.StatusOK, r)
  82. } else {
  83. //错误返回
  84. errcode := xerr.SERVER_COMMON_ERROR
  85. errmsg := "服务器开小差啦,稍后再来试一试"
  86. causeErr := errors.Cause(err) // err类型
  87. if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型
  88. //自定义CodeError
  89. errcode = e.GetErrCode()
  90. errmsg = e.GetErrMsg()
  91. } else {
  92. if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误
  93. grpcCode := uint32(gstatus.Code())
  94. if xerr.IsCodeErr(grpcCode) { //区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端
  95. errcode = grpcCode
  96. errmsg = gstatus.Message()
  97. }
  98. }
  99. }
  100. logx.WithContext(r.Context()).Errorf("【GATEWAY-ERR】 : %+v ", err)
  101. httpx.WriteJson(w, http.StatusUnauthorized, Error(errcode, errmsg, r.Context()))
  102. }
  103. }
  104. // http 参数错误返回
  105. func ParamErrorResult(r *http.Request, w http.ResponseWriter, err error) {
  106. errMsg := fmt.Sprintf("%s ,%s", xerr.MapErrMsg(xerr.REUQEST_PARAM_ERROR), err.Error())
  107. httpx.WriteJson(w, http.StatusBadRequest, Error(xerr.REUQEST_PARAM_ERROR, errMsg, r.Context()))
  108. }
  109. func traceIDFromContext(ctx context.Context) string {
  110. spanCtx := trace.SpanContextFromContext(ctx)
  111. if spanCtx.HasTraceID() {
  112. return spanCtx.TraceID().String()
  113. }
  114. return ""
  115. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.