Skip to content

Commit 22861cb

Browse files
committed
Return original error on resources.GetRemote retry timeouts
See #11327
1 parent 16da1ad commit 22861cb

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

resources/resource_factories/create/integration_test.go

+37-16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222
"testing"
2323

24+
qt "github.com/frankban/quicktest"
2425
"github.com/gohugoio/hugo/hugolib"
2526
)
2627

@@ -67,10 +68,10 @@ func TestGetRemoteRetry(t *testing.T) {
6768
t.Parallel()
6869

6970
temporaryHTTPCodes := []int{408, 429, 500, 502, 503, 504}
70-
numPages := 30
71+
numPages := 20
7172

7273
handler := func(w http.ResponseWriter, r *http.Request) {
73-
if rand.Intn(4) == 0 {
74+
if rand.Intn(3) == 0 {
7475
w.WriteHeader(temporaryHTTPCodes[rand.Intn(len(temporaryHTTPCodes))])
7576
return
7677
}
@@ -81,9 +82,10 @@ func TestGetRemoteRetry(t *testing.T) {
8182
srv := httptest.NewServer(http.HandlerFunc(handler))
8283
t.Cleanup(func() { srv.Close() })
8384

84-
files := `
85+
filesTemplate := `
8586
-- hugo.toml --
8687
disableKinds = ["home", "taxonomy", "term"]
88+
timeout = "TIMEOUT"
8789
[security]
8890
[security.http]
8991
urls = ['.*']
@@ -93,7 +95,7 @@ mediaTypes = ['text/plain']
9395
{{ $opts := dict }}
9496
{{ with resources.GetRemote $url $opts }}
9597
{{ with .Err }}
96-
{{ errorf "Unable to get remote resource: %s" . }}
98+
{{ errorf "Got Err: %s. Data: %v" . .Data }}
9799
{{ else }}
98100
Content: {{ .Content }}
99101
{{ end }}
@@ -103,22 +105,41 @@ mediaTypes = ['text/plain']
103105
`
104106

105107
for i := 0; i < numPages; i++ {
106-
files += fmt.Sprintf("-- content/post/p%d.md --\n", i)
108+
filesTemplate += fmt.Sprintf("-- content/post/p%d.md --\n", i)
107109
}
108110

109-
files = strings.ReplaceAll(files, "URL", srv.URL)
111+
filesTemplate = strings.ReplaceAll(filesTemplate, "URL", srv.URL)
110112

111-
b := hugolib.NewIntegrationTestBuilder(
112-
hugolib.IntegrationTestConfig{
113-
T: t,
114-
TxtarString: files,
115-
},
116-
)
113+
t.Run("OK", func(t *testing.T) {
114+
files := strings.ReplaceAll(filesTemplate, "TIMEOUT", "60s")
115+
b := hugolib.NewIntegrationTestBuilder(
116+
hugolib.IntegrationTestConfig{
117+
T: t,
118+
TxtarString: files,
119+
},
120+
)
117121

118-
b.Build()
122+
b.Build()
119123

120-
for i := 0; i < numPages; i++ {
121-
b.AssertFileContent(fmt.Sprintf("public/post/p%d/index.html", i), fmt.Sprintf("Content: Response for /post/p%d/.", i))
122-
}
124+
for i := 0; i < numPages; i++ {
125+
b.AssertFileContent(fmt.Sprintf("public/post/p%d/index.html", i), fmt.Sprintf("Content: Response for /post/p%d/.", i))
126+
}
127+
})
128+
129+
t.Run("Timeout", func(t *testing.T) {
130+
files := strings.ReplaceAll(filesTemplate, "TIMEOUT", "100ms")
131+
b, err := hugolib.NewIntegrationTestBuilder(
132+
hugolib.IntegrationTestConfig{
133+
T: t,
134+
TxtarString: files,
135+
},
136+
).BuildE()
137+
138+
b.Assert(err, qt.IsNotNil)
139+
b.AssertLogContains("Got Err")
140+
b.AssertLogContains("Retry timeout")
141+
b.AssertLogContains("ContentLength:0")
142+
143+
})
123144

124145
}

resources/resource_factories/create/remote.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
159159
if start.IsZero() {
160160
start = time.Now()
161161
} else if d := time.Since(start) + nextSleep; d >= c.rs.Cfg.Timeout() {
162-
return nil, fmt.Errorf("timeout (configured to %s) fetching remote resource %s: last error: %w", c.rs.Cfg.Timeout(), uri, err)
162+
c.rs.Logger.Errorf("Retry timeout (configured to %s) fetching remote resource.", c.rs.Cfg.Timeout())
163+
return nil, err
163164
}
164165
time.Sleep(nextSleep)
165166
if nextSleep < nextSleepLimit {

0 commit comments

Comments
 (0)