Skip to content

Commit 2a5b40b

Browse files
authored
Only set cache control on valid responses (#30)
If the delegate RoundTripper returns an error and no response, do not try to access the response headers.
1 parent 2dbd6a6 commit 2a5b40b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

githubapp/client_creator.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,17 @@ func cacheControl(alwaysValidate bool) ClientMiddleware {
340340
if !alwaysValidate {
341341
return next
342342
}
343+
344+
// Force validation to occur when the cache is disabled by setting
345+
// max-age=0 so that cached results will always appear to be stale
343346
return roundTripperFunc(func(r *http.Request) (*http.Response, error) {
344347
resp, err := next.RoundTrip(r)
345-
346-
// Force validation to occur when the cache is disabled by setting max-age=0,
347-
// as the cache results will always appear as stale
348-
cacheControl := resp.Header.Get("Cache-Control")
349-
if cacheControl != "" {
350-
newCacheControl := maxAgeRegex.ReplaceAllString(cacheControl, "max-age=0")
351-
resp.Header.Set("Cache-Control", newCacheControl)
348+
if resp != nil {
349+
cacheControl := resp.Header.Get("Cache-Control")
350+
if cacheControl != "" {
351+
newCacheControl := maxAgeRegex.ReplaceAllString(cacheControl, "max-age=0")
352+
resp.Header.Set("Cache-Control", newCacheControl)
353+
}
352354
}
353355
return resp, err
354356
})

0 commit comments

Comments
 (0)