From 67422b4912aa842ca9e99b8f7907c99b7cc43f84 Mon Sep 17 00:00:00 2001 From: Robert Pirtle Date: Thu, 8 Feb 2024 12:40:07 -0800 Subject: [PATCH] onErrStatus(status) -> onErrorHandler(status, body) --- service/batchmdw/batch_processor.go | 2 +- service/batchmdw/fake_response_writer.go | 16 +++++++++------- service/cachemdw/cache.go | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/service/batchmdw/batch_processor.go b/service/batchmdw/batch_processor.go index 23d4163..7baa172 100644 --- a/service/batchmdw/batch_processor.go +++ b/service/batchmdw/batch_processor.go @@ -120,7 +120,7 @@ func (bp *BatchProcessor) applyHeaders(h http.Header) { } // SetErrStatus tracks an error status code if any request returns a non-200 response -func (bp *BatchProcessor) setErrStatus(status int) { +func (bp *BatchProcessor) setErrStatus(status int, _ *bytes.Buffer) { bp.mu.Lock() defer bp.mu.Unlock() bp.status = status diff --git a/service/batchmdw/fake_response_writer.go b/service/batchmdw/fake_response_writer.go index eedfa8d..9d0f75f 100644 --- a/service/batchmdw/fake_response_writer.go +++ b/service/batchmdw/fake_response_writer.go @@ -5,6 +5,8 @@ import ( "net/http" ) +type ErrorHandler = func(status int, rawRes *bytes.Buffer) + // fakeResponseWriter is a custom implementation of http.ResponseWriter that writes all content // to a buffer. type fakeResponseWriter struct { @@ -12,18 +14,18 @@ type fakeResponseWriter struct { body *bytes.Buffer // header is the response headers for the current request header http.Header - // onErrStatus is a method for handling non-OK status responses - onErrStatus func(status int) + // onErrorHandler is a method for handling non-OK status responses + onErrorHandler ErrorHandler } var _ http.ResponseWriter = &fakeResponseWriter{} // newFakeResponseWriter creates a new fakeResponseWriter that wraps the provided buffer. -func newFakeResponseWriter(buf *bytes.Buffer, onErrStatus func(status int)) *fakeResponseWriter { +func newFakeResponseWriter(buf *bytes.Buffer, onErrorHandler ErrorHandler) *fakeResponseWriter { return &fakeResponseWriter{ - header: make(http.Header), - body: buf, - onErrStatus: onErrStatus, + header: make(http.Header), + body: buf, + onErrorHandler: onErrorHandler, } } @@ -45,6 +47,6 @@ func (w *fakeResponseWriter) Header() http.Header { // it overrides the WriteHeader method to prevent proxied requests from having finalized headers func (w *fakeResponseWriter) WriteHeader(status int) { if status != http.StatusOK { - w.onErrStatus(status) + w.onErrorHandler(status, w.body) } } diff --git a/service/cachemdw/cache.go b/service/cachemdw/cache.go index 1568942..8110d5b 100644 --- a/service/cachemdw/cache.go +++ b/service/cachemdw/cache.go @@ -80,8 +80,8 @@ func IsCacheable( logger *logging.ServiceLogger, req *decode.EVMRPCRequestEnvelope, ) bool { - // TODO: technically, we _could_ cache the "invalid request" response for `null` requests... - // however, doing so will may result in different than expected responses for batch requests + // technically, we _could_ cache the "invalid request" response for `null` requests... + // however, doing so will result in different than expected responses for batch requests // ie. {error} response vs [{error}] (the not-list is expected) if req == nil { return false