Skip to content

Commit df33e57

Browse files
committed
refactor: no more error to validation handler
1 parent 5b63106 commit df33e57

3 files changed

Lines changed: 18 additions & 22 deletions

File tree

internal/validationresponsehandler.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ type ValidationResponseHandler interface {
2424
ctx RevalidationContext,
2525
req *http.Request,
2626
resp *http.Response,
27-
err error,
2827
) (*http.Response, error)
2928
}
3029

@@ -75,13 +74,7 @@ func (r *validationResponseHandler) HandleValidationResponse(
7574
ctx RevalidationContext,
7675
req *http.Request,
7776
resp *http.Response,
78-
err error,
7977
) (*http.Response, error) {
80-
81-
if err != nil {
82-
return nil, err
83-
}
84-
8578
if req.Method == http.MethodGet && resp.StatusCode == http.StatusNotModified {
8679
// RFC 9111 §4.3.3 Handling Validation Responses (304 Not Modified)
8780
// RFC 9111 §4.3.4 Freshening Stored Responses upon Validation

roundtripper.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ func (r *transport) handleCacheHit(
340340
revalidate:
341341
req = withConditionalHeaders(req, stored.Data.Header)
342342
resp, start, end, err := r.roundTripTimed(req)
343+
if err != nil {
344+
return nil, err
345+
}
343346
ctx := internal.RevalidationContext{
344347
URLKey: urlKey,
345348
Start: start,
@@ -350,7 +353,7 @@ revalidate:
350353
RefIndex: refIndex,
351354
Freshness: freshness,
352355
}
353-
return r.vrh.HandleValidationResponse(ctx, req, resp, err)
356+
return r.vrh.HandleValidationResponse(ctx, req, resp)
354357
}
355358

356359
func (r *transport) serveFromCache(
@@ -441,7 +444,7 @@ func (r *transport) backgroundRevalidate(
441444
Freshness: freshness,
442445
}
443446
//nolint:bodyclose // The response is not used, so we don't need to close it.
444-
_, err = r.vrh.HandleValidationResponse(revalCtx, req, resp, nil)
447+
_, err = r.vrh.HandleValidationResponse(revalCtx, req, resp)
445448
errc <- err
446449
}()
447450

roundtripper_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func mockTransport(fields func(rt *transport)) *transport {
6464
ci: &internal.MockCacheInvalidator{},
6565
rs: &internal.MockResponseStorer{},
6666
vrh: &internal.MockValidationResponseHandler{
67-
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error) {
68-
return resp, err
67+
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response) (*http.Response, error) {
68+
return resp, nil
6969
},
7070
},
7171
clock: &internal.MockClock{NowResult: time.Now()},
@@ -224,9 +224,9 @@ func Test_transport_CacheHit_MustRevalidate_Stale(t *testing.T) {
224224
},
225225
}
226226
rt.vrh = &internal.MockValidationResponseHandler{
227-
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error) {
227+
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response) (*http.Response, error) {
228228
mockVHCalled = true
229-
return resp, err
229+
return resp, nil
230230
},
231231
}
232232
})
@@ -259,9 +259,9 @@ func Test_transport_CacheHit_NoCacheUnqualified(t *testing.T) {
259259
},
260260
}
261261
rt.vrh = &internal.MockValidationResponseHandler{
262-
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error) {
262+
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response) (*http.Response, error) {
263263
mockVHCalled = true
264-
return resp, err
264+
return resp, nil
265265
},
266266
}
267267
})
@@ -539,10 +539,10 @@ func Test_transport_RevalidationPath(t *testing.T) {
539539
},
540540
}
541541
rt.vrh = &internal.MockValidationResponseHandler{
542-
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error) {
542+
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response) (*http.Response, error) {
543543
mockVHCalled = true
544544
internal.CacheStatusRevalidated.ApplyTo(resp.Header)
545-
return resp, err
545+
return resp, nil
546546
},
547547
}
548548
})
@@ -596,9 +596,9 @@ func Test_transport_SWR_NormalPath(t *testing.T) {
596596
rt.clock = &internal.MockClock{NowResult: base.Add(5 * time.Second), SinceResult: 0}
597597
rt.siep = &internal.MockStaleIfErrorPolicy{}
598598
rt.vrh = &internal.MockValidationResponseHandler{
599-
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error) {
599+
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response) (*http.Response, error) {
600600
revalidateCalled <- struct{}{} // Signal that revalidation was called
601-
return resp, err
601+
return resp, nil
602602
},
603603
}
604604
rt.swrTimeout = DefaultSWRTimeout
@@ -669,7 +669,7 @@ func Test_transport_SWR_NormalPathAndError(t *testing.T) {
669669
rt.clock = &internal.MockClock{NowResult: base.Add(5 * time.Second), SinceResult: 0}
670670
rt.swrTimeout = swrTimeout
671671
rt.vrh = &internal.MockValidationResponseHandler{
672-
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error) {
672+
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response) (*http.Response, error) {
673673
defer func() { revalidateCalled <- struct{}{} }() // Signal that revalidation was called
674674
return nil, errors.New("revalidation error")
675675
},
@@ -736,9 +736,9 @@ func Test_transport_SWR_Timeout(t *testing.T) {
736736
rt.clock = &internal.MockClock{NowResult: base.Add(5 * time.Second), SinceResult: 0}
737737
rt.swrTimeout = swrTimeout
738738
rt.vrh = &internal.MockValidationResponseHandler{
739-
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error) {
739+
HandleValidationResponseFunc: func(ctx internal.RevalidationContext, req *http.Request, resp *http.Response) (*http.Response, error) {
740740
revalidateCalled <- struct{}{} // Signal that revalidation was called
741-
return resp, err
741+
return resp, nil
742742
},
743743
}
744744
rt.upstream = &internal.MockRoundTripper{

0 commit comments

Comments
 (0)