Skip to content

Commit

Permalink
fix: makes UnexpectedEOF error in graphql log with error level (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas1993 authored Jan 19, 2024
1 parent 0bb8c06 commit c451a09
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 52 deletions.
9 changes: 7 additions & 2 deletions internal/graphql/errorPresenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package graphql
import (
"context"
"errors"
"io"

"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog"
Expand All @@ -17,11 +18,15 @@ func errorPresenter(ctx context.Context, err error) *gqlerror.Error {
return gqlErr
}
}

requestID := middleware.GetReqID(ctx)

lvl := zerolog.PanicLevel
if errors.Is(err, io.ErrUnexpectedEOF) {
lvl = zerolog.ErrorLevel
}

logger := log.Ctx(ctx).With().Stack().Logger()
logger.WithLevel(zerolog.PanicLevel).
logger.WithLevel(lvl).
Err(err).
Str("requestID", requestID).
Msg("response not provided")
Expand Down
50 changes: 0 additions & 50 deletions internal/router/chi/middlewares.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package chi

import (
"bytes"
"fmt"
"io"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/gothunder/thunder/internal/log"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
)

func defaultMiddlewares(r *chi.Mux, logger *zerolog.Logger) {
Expand All @@ -25,48 +19,4 @@ func defaultMiddlewares(r *chi.Mux, logger *zerolog.Logger) {

// Add a logger instance to the context with some default values
r.Use(log.Middleware(logger))

// Logs the request
r.Use(requestDumpMiddleware())
}

func requestDumpMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

var strBody string

body := r.Body
bytesBody, err := io.ReadAll(body)
if err != nil {
strBody = fmt.Sprintf("error reading body: %s", err.Error())
} else {
// Rewrites body to allow for further reading
r.Body = io.NopCloser(bytes.NewBuffer(bytesBody))

strBody = string(bytesBody)
}

logger := zlog.Ctx(r.Context())

headerBuffer := bytes.Buffer{}
var strHeader string

err = r.Header.Write(&headerBuffer)
if err != nil {
strHeader = fmt.Sprintf("error writing headers to string: %s", err.Error())
} else {
strHeader = headerBuffer.String()
}

logger.Debug().
Str("headers", strHeader).
Str("body", strBody).
Str("requestID", middleware.GetReqID(r.Context())).
Msg("logging request")

next.ServeHTTP(w, r)
})
}

}

0 comments on commit c451a09

Please sign in to comment.