Skip to content

exuan/gin-wrap-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wrap gin handler response

gin HandlerFunc 很容易出现忘记return问题:

func(c *gin.Context) {
    var json struct {
    Value string `json:"value" binding:"required"`
    }
    
    if err := c.Bind(&json); err != nil {
        c.String(http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
    }
    
    c.JSON(200, gin.H{
        "message": "pong",
    })
}

可以通过封装一个wrap统一处理响应和错误。附件响应可以不走wrap或者通过解析Metadata处理。

func wrap(f func(c *gin.Context) (interface{}, error)) gin.HandlerFunc {
	return func(c *gin.Context) {
		res, err := f(c)

		// if nil, then return ''
		if res == nil {
			res = ``
		}

		ret := Ret{
			Data: res,
		}
		code := http.StatusOK

		if err != nil {
			e := errors.FromError(err)
			// wrap http status code
			if e.Code > 0 && e.Code <= 600 {
				code = e.Code
			}

			ret.Msg = e.Msg
			ret.Code = e.Code
		}

		c.JSON(code, ret)
	}
}

感谢

How to wrap route handler function gin.HandlerFunc
gin
go-kratos

About

gin wrap handler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages