Browse Source

503 Test

master
dark 1 year ago
parent
commit
84f75e02b7
  1. 0
      apis/teThoroughlyRegister.api
  2. 123
      common/httpresult.go
  3. 7
      demo.api
  4. BIN
      demo.exe
  5. 1
      go.mod
  6. 2
      go.sum
  7. 5
      internal/handler/routes.go
  8. 25
      internal/handler/test503handler.go
  9. 29
      internal/logic/test503logic.go

0
apifile/teThoroughlyRegister.api → apis/teThoroughlyRegister.api

123
common/httpresult.go

@ -0,0 +1,123 @@
package httpresult
import (
// "go-scaffold/common/logwriter"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
)
const defaultErrCode = 1001
const defaultSuccessCode = 200
type CodeError struct {
Code int `json:"code"`
Message string `json:"msg"`
Success bool `json:"success"`
ErrMsg interface{} `json:"err_msg"`
}
type CodeErrorResponse struct {
Code int `json:"code"`
Message string `json:"msg"`
Success bool `json:"success"`
ErrMsg interface{} `json:"err_msg"`
}
type CodeSuccessResponse struct {
Code int `json:"code"`
Message string `json:"msg"`
Success bool `json:"success"`
Data interface{} `json:"data"`
}
// http 错误返回
func ErrorResult(msg string, err_msg interface{}) error {
return &CodeError{Code: defaultErrCode, Message: msg, Success: false, ErrMsg: err_msg}
}
// http 默认错误返回
func ErrorResultDefault(msg string) error {
return ErrorResult(msg, "")
}
// 错误方法返回
func (e *CodeError) Error() string {
return e.Message
}
// 错误数据赋值
func (e *CodeError) ErrorData() *CodeErrorResponse {
return &CodeErrorResponse{
Code: e.Code,
Message: e.Message,
Success: e.Success,
ErrMsg: e.ErrMsg,
}
}
// http 成功返回
func SuccessResult(msg string, data interface{}) (res *CodeSuccessResponse) {
return &CodeSuccessResponse{Code: defaultSuccessCode, Message: msg, Success: true, Data: data}
}
// http 参数错误返回
func ParamErrorResult(r *http.Request, w http.ResponseWriter, err error) {
msg := &CodeErrorResponse{
Code: 403,
Message: "请求参数错误",
Success: false,
ErrMsg: err.Error(),
}
httpx.WriteJson(w, http.StatusOK, msg)
}
// http 通用错误返回
func CommonErrorResult(r *http.Request, w http.ResponseWriter, err error) {
if r.PostForm != nil {
// logwriter.ErrorLogWriter(r.Context(), r.URL.Path, err.Error(), r.PostForm)
} else {
// logwriter.ErrorLogWriter(r.Context(), r.URL.Path, err.Error(), nil)
}
msg := &CodeErrorResponse{
Code: defaultErrCode,
Message: "请求失败",
Success: false,
ErrMsg: err.Error(),
}
httpx.WriteJson(w, http.StatusOK, msg)
}
// http auth错误返回
func AuthErrorResult(r *http.Request, w http.ResponseWriter, err error) {
msg := &CodeErrorResponse{
Code: 401,
Message: "鉴权失败",
Success: false,
ErrMsg: err.Error(),
}
httpx.WriteJson(w, http.StatusUnauthorized, msg)
}
// http 权限错误返回
func PermissionErrorResult(r *http.Request, w http.ResponseWriter, err error) {
msg := &CodeErrorResponse{
Code: 403,
Message: "权限校验失败",
Success: false,
ErrMsg: err.Error(),
}
httpx.WriteJson(w, 403, msg)
}
// http 503错误返回
func ServiceUnavailableErrorResult(w http.ResponseWriter, massage string) {
msg := &CodeErrorResponse{
Code: 503,
Message: "服务不可用",
Success: false,
ErrMsg: nil,
}
httpx.WriteJson(w, 503, msg)
}

7
demo.api

@ -1,5 +1,5 @@
syntax = "v1"
import "/apifile/teThoroughlyRegister.api"
import "/apis/teThoroughlyRegister.api"
type Request {
Name string `path:"name,options=you|me"`
@ -38,4 +38,9 @@ service demo-api {
@handler EsBusinessPressureTestHandler
post /es/businessPressureTest returns (Response)
@doc(
summary: "测试返回503",
)
@handler Test503Handler
get /test503 returns (Response)
}

BIN
demo.exe

Binary file not shown.

1
go.mod

@ -9,6 +9,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/lib/pq v1.10.9
github.com/zeromicro/go-zero v1.6.3
github.com/zeromicro/x v0.0.0-20230424055333-01c7fb9548d4
)
require (

2
go.sum

@ -91,6 +91,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE=
github.com/zeromicro/go-zero v1.6.3 h1:OL0NnHD5LdRNDolfcK9vUkJt7K8TcBE3RkzfM8poOVw=
github.com/zeromicro/go-zero v1.6.3/go.mod h1:XZL435ZxVi9MSXXtw2MRQhHgx6OoX3++MRMOE9xU70c=
github.com/zeromicro/x v0.0.0-20230424055333-01c7fb9548d4 h1:EGNpFLf3u5vO8XfYWTSz9Nj7aAqquC40RbCWE6EPRW8=
github.com/zeromicro/x v0.0.0-20230424055333-01c7fb9548d4/go.mod h1:5TP11tc1RHPCi5C/KDL0kIB0KgJAb9FB3ChpT/qM/jA=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 h1:D7UpUy2Xc2wsi1Ras6V40q806WM07rqoCWzXu7Sqy+4=

5
internal/handler/routes.go

@ -27,6 +27,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/es/businessPressureTest",
Handler: EsBusinessPressureTestHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/test503",
Handler: Test503Handler(serverCtx),
},
},
)

25
internal/handler/test503handler.go

@ -0,0 +1,25 @@
package handler
import (
"net/http"
"github.com/zeromicro/x/errors"
"demo/internal/logic"
"demo/internal/svc"
"github.com/zeromicro/go-zero/rest/httpx"
)
func Test503Handler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logic.NewTest503Logic(r.Context(), svcCtx)
resp, err := l.Test503()
if err != nil {
// httpx.ErrorCtx(r.Context(), w, err)
httpx.WriteJson(w, http.StatusServiceUnavailable, errors.New(503, "503"))
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

29
internal/logic/test503logic.go

@ -0,0 +1,29 @@
package logic
import (
"context"
httpresult "demo/common"
"demo/internal/svc"
"demo/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type Test503Logic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewTest503Logic(ctx context.Context, svcCtx *svc.ServiceContext) *Test503Logic {
return &Test503Logic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *Test503Logic) Test503() (resp *types.Response, err error) {
// todo: add your logic here and delete this line
return nil, httpresult.ErrorResultDefault("5031")
}
Loading…
Cancel
Save