mirror of https://gitee.com/answerdev/answer.git
feat(err): add error handler function
This commit is contained in:
parent
fad8acbe16
commit
0954b5ff11
|
@ -59,3 +59,18 @@ func BindAndCheck(ctx *gin.Context, data interface{}) bool {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// BindAndCheckReturnErr bind request and check
|
||||
func BindAndCheckReturnErr(ctx *gin.Context, data interface{}) (errFields []*validator.FormErrorField) {
|
||||
lang := GetLang(ctx)
|
||||
ctx.Set(constant.AcceptLanguageFlag, lang)
|
||||
if err := ctx.ShouldBind(data); err != nil {
|
||||
log.Errorf("http_handle BindAndCheck fail, %s", err.Error())
|
||||
HandleResponse(ctx, myErrors.New(http.StatusBadRequest, reason.RequestFormatError), nil)
|
||||
ctx.Abort()
|
||||
return nil
|
||||
}
|
||||
|
||||
errFields, _ = validator.GetValidatorByLang(lang.Abbr()).Check(data)
|
||||
return errFields
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/answerdev/answer/pkg/converter"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// QuestionController question controller
|
||||
|
@ -287,7 +288,8 @@ func (qc *QuestionController) AddQuestion(ctx *gin.Context) {
|
|||
// @Router /answer/api/v1/question [put]
|
||||
func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
|
||||
req := &schema.QuestionUpdate{}
|
||||
if handler.BindAndCheck(ctx, req) {
|
||||
errFields := handler.BindAndCheckReturnErr(ctx, req)
|
||||
if ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||
|
@ -311,6 +313,9 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
// TODO: pass errFields and return errors
|
||||
log.Info(errFields)
|
||||
|
||||
resp, err := qc.questionService.UpdateQuestion(ctx, req)
|
||||
if err != nil {
|
||||
handler.HandleResponse(ctx, err, resp)
|
||||
|
|
Loading…
Reference in New Issue