Skip to content

Commit

Permalink
onErrStatus(status) -> onErrorHandler(status, body)
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtleshell committed Feb 8, 2024
1 parent 220e764 commit 67422b4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion service/batchmdw/batch_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions service/batchmdw/fake_response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ 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 {
// body is the response body for the current request
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,
}
}

Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 67422b4

Please sign in to comment.