Skip to content

Commit

Permalink
[perf] 过滤验证类错误
Browse files Browse the repository at this point in the history
  • Loading branch information
runs committed Feb 26, 2023
1 parent 651ccac commit 51bde41
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions internal/app/api/middlewares/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,32 @@ func (e *ErrorInterceptor) Handler(ctx *gin.Context) {
return
}

headers, _ := json.Marshal(ctx.Request.Header)
logs := []zap.Field{
zap.String("request.method", ctx.Request.Method),
zap.String("request.url", ctx.Request.URL.String()),
zap.ByteString("request.headers", headers),
zap.String("errors", ginErrorsToString(ctx.Errors)),
}
e.logger.Error("request error:", logs...)

code, msg := -1, "服务器异常,请稍后再试"
code, msg, notLog := -1, "服务器异常,请稍后再试", false
for i := len(ctx.Errors) - 1; i >= 0; i-- {
err := ctx.Errors[i]
if appError, ok := errors.Cause(err.Err).(*apperrors.AppError); ok {
code = appError.Code
switch appError.ResponseType {
case apperrors.ValidationError:
msg = appError.Message
case apperrors.AuthError:
case apperrors.ValidationError, apperrors.AuthError:
msg = appError.Message
notLog = true
}
break
}
}

if !notLog {
headers, _ := json.Marshal(ctx.Request.Header)
logs := []zap.Field{
zap.String("request.method", ctx.Request.Method),
zap.String("request.url", ctx.Request.URL.String()),
zap.ByteString("request.headers", headers),
zap.String("errors", ginErrorsToString(ctx.Errors)),
}

e.logger.Error("request error:", logs...)
}

ctx.JSON(http.StatusOK, help.FailureJson(code, msg, nil))
}()
ctx.Next()
Expand Down

0 comments on commit 51bde41

Please sign in to comment.