Skip to content

Commit

Permalink
fix filter content type (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee authored Jul 9, 2024
1 parent d244a1a commit c8443b1
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions internal/middlewares/middleware_accesslog/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package middleware_accesslog
import (
"context"
"fmt"
"strings"
"time"

"github.com/gofiber/utils"
Expand Down Expand Up @@ -42,8 +41,6 @@ func (l LogMiddleware) Middleware(next lava.HandlerFunc) lava.HandlerFunc {
return func(ctx context.Context, req lava.Request) (rsp lava.Response, gErr error) {
now := time.Now()

logOpts := handleLogOption(req.Header().PeekAll("X-Log-Option"))

evt := log.NewEvent()
referer := utils.UnsafeString(req.Header().Referer())
if referer != "" {
Expand All @@ -67,8 +64,9 @@ func (l LogMiddleware) Middleware(next lava.HandlerFunc) lava.HandlerFunc {

// 错误和panic处理
defer func() {
if !generic.IsNil(gErr) || logOpts["all"] {
if !strings.HasPrefix(req.ContentType(), "multipart/form-data") {
if !generic.IsNil(gErr) {
logOpts := handleLogOption(req.Header().PeekAll("X-Log-Option"))
if logOpts["all"] {
evt.Any("req_body", req.Payload())
evt.Bytes("req_header", req.Header().Header())
if rsp != nil {
Expand Down Expand Up @@ -104,30 +102,41 @@ func (l LogMiddleware) Middleware(next lava.HandlerFunc) lava.HandlerFunc {
e = l.logger.Err(gErr).Func(log.WithEvent(evt))

pb := errutil.ParseError(gErr)
if pb.Trace == nil {
pb.Trace = new(errorpb.ErrTrace)
{
if pb.Trace == nil {
pb.Trace = new(errorpb.ErrTrace)
}
pb.Trace.Operation = req.Operation()
pb.Trace.Service = req.Service()
pb.Trace.Version = version.Version()
}
pb.Trace.Operation = req.Operation()
pb.Trace.Service = req.Service()
pb.Trace.Version = version.Version()

if pb.Msg != nil {
pb.Msg = new(errorpb.ErrMsg)
}
pb.Msg.Msg = gErr.Error()
pb.Msg.Detail = fmt.Sprintf("%#v", gErr)
if pb.Msg.Tags == nil {
pb.Msg.Tags = make(map[string]string)
{
if pb.Msg != nil {
pb.Msg = new(errorpb.ErrMsg)
}
pb.Msg.Msg = gErr.Error()
pb.Msg.Detail = fmt.Sprintf("%#v", gErr)
if pb.Msg.Tags == nil {
pb.Msg.Tags = make(map[string]string)
}
}

if pb.Code.Message == "" {
pb.Code.Message = gErr.Error()
}
{
if pb.Code.Message == "" {
pb.Code.Message = gErr.Error()
}

if pb.Code.StatusCode == errorpb.Code_OK {
log.Warn(ctx).Any("code", pb.Code).Msg("grpc response error with status code is 0")
}

if pb.Code.Code == 0 {
pb.Code.Code = int32(errutil.GrpcCodeToHTTP(codes.Code(pb.Code.StatusCode)))
pb.Code.StatusCode = errorpb.Code_Internal
if pb.Code.Code == 0 {
pb.Code.Code = int32(errutil.GrpcCodeToHTTP(codes.Code(pb.Code.StatusCode)))
pb.Code.StatusCode = errorpb.Code_Internal
}
}

gErr = errutil.ConvertErr2Status(pb).Err()
}
e.Msg("record request")
Expand Down

0 comments on commit c8443b1

Please sign in to comment.