diff --git a/service/cachemdw/caching_middleware.go b/service/cachemdw/caching_middleware.go index 177bb6b..087daf3 100644 --- a/service/cachemdw/caching_middleware.go +++ b/service/cachemdw/caching_middleware.go @@ -1,9 +1,12 @@ package cachemdw import ( + "bytes" + "io" "net/http" "github.com/kava-labs/kava-proxy-service/decode" + "github.com/kava-labs/kava-proxy-service/logging" ) // CachingMiddleware returns kava-proxy-service compatible middleware which works in the following way: @@ -35,11 +38,7 @@ func (c *ServiceCache) CachingMiddleware( req := r.Context().Value(c.decodedRequestContextKey) decodedReq, ok := (req).(*decode.EVMRPCRequestEnvelope) if !ok { - c.Logger.Error(). - Str("method", r.Method). - Str("url", r.URL.String()). - Str("host", r.Host). - Msg("can't cast request to *EVMRPCRequestEnvelope type") + LogCannotCastRequestError(c.ServiceLogger, r) next.ServeHTTP(w, r) return @@ -67,6 +66,26 @@ func (c *ServiceCache) CachingMiddleware( } } +func LogCannotCastRequestError(logger *logging.ServiceLogger, r *http.Request) { + var bodyCopy bytes.Buffer + tee := io.TeeReader(r.Body, &bodyCopy) + // read all body from reader into bodyBytes, and copy into bodyCopy + bodyBytes, err := io.ReadAll(tee) + if err != nil { + logger.Error().Err(err).Msg("can't read lrw.body") + } + + // replace empty body reader with fresh copy + r.Body = io.NopCloser(&bodyCopy) + + logger.Error(). + Str("method", r.Method). + Str("url", r.URL.String()). + Str("host", r.Host). + Str("body", string(bodyBytes)). + Msg("can't cast request to *EVMRPCRequestEnvelope type") +} + // getHeadersToCache gets header map which has to be cached along with EVM JSON-RPC response func getHeadersToCache(w http.ResponseWriter, whitelistedHeaders []string) map[string]string { headersToCache := make(map[string]string, 0) diff --git a/service/cachemdw/is_cached_middleware.go b/service/cachemdw/is_cached_middleware.go index 483aa11..e41b4c9 100644 --- a/service/cachemdw/is_cached_middleware.go +++ b/service/cachemdw/is_cached_middleware.go @@ -47,11 +47,7 @@ func (c *ServiceCache) IsCachedMiddleware( req := r.Context().Value(c.decodedRequestContextKey) decodedReq, ok := (req).(*decode.EVMRPCRequestEnvelope) if !ok { - c.Logger.Error(). - Str("method", r.Method). - Str("url", r.URL.String()). - Str("host", r.Host). - Msg("can't cast request to *EVMRPCRequestEnvelope type") + LogCannotCastRequestError(c.ServiceLogger, r) next.ServeHTTP(w, r.WithContext(uncachedContext)) return diff --git a/service/middleware.go b/service/middleware.go index 28da241..4251074 100644 --- a/service/middleware.go +++ b/service/middleware.go @@ -157,23 +157,7 @@ func createProxyRequestMiddleware(next http.Handler, config config.Config, servi req := r.Context().Value(DecodedRequestContextKey) decodedReq, ok := (req).(*decode.EVMRPCRequestEnvelope) if !ok { - var bodyCopy bytes.Buffer - tee := io.TeeReader(r.Body, &bodyCopy) - // read all body from reader into bodyBytes, and copy into bodyCopy - bodyBytes, err := io.ReadAll(tee) - if err != nil { - serviceLogger.Error().Err(err).Msg("can't read lrw.body") - } - - // replace empty body reader with fresh copy - r.Body = io.NopCloser(&bodyCopy) - - serviceLogger.Logger.Error(). - Str("method", r.Method). - Str("url", r.URL.String()). - Str("host", r.Host). - Str("body", string(bodyBytes)). - Msg("can't cast request to *EVMRPCRequestEnvelope type") + cachemdw.LogCannotCastRequestError(serviceLogger, r) // if we can't get decoded request then assign it empty structure to avoid panics decodedReq = new(decode.EVMRPCRequestEnvelope)