resources: Replace error handling in GetRemote with try (note)

Closes #13216
This commit is contained in:
Bjørn Erik Pedersen
2025-01-05 15:43:18 +01:00
parent 4ea94c451d
commit 0918e087ec
16 changed files with 108 additions and 223 deletions

View File

@@ -31,18 +31,17 @@ func TestGetRemoteHead(t *testing.T) {
[security.http]
methods = ['(?i)GET|POST|HEAD']
urls = ['.*gohugo\.io.*']
-- layouts/index.html --
{{ $url := "https://gohugo.io/img/hugo.png" }}
{{ $opts := dict "method" "head" }}
{{ with resources.GetRemote $url $opts }}
{{ with try (resources.GetRemote $url $opts) }}
{{ with .Err }}
{{ errorf "Unable to get remote resource: %s" . }}
{{ else }}
{{ else with .Value }}
Head Content: {{ .Content }}. Head Data: {{ .Data }}
{{ end }}
{{ else }}
{{ else }}
{{ errorf "Unable to get remote resource: %s" $url }}
{{ end }}
{{ end }}
`
@@ -90,14 +89,15 @@ mediaTypes = ['text/plain']
-- layouts/_default/single.html --
{{ $url := printf "%s%s" "URL" .RelPermalink}}
{{ $opts := dict }}
{{ with resources.GetRemote $url $opts }}
{{ with try (resources.GetRemote $url $opts) }}
{{ with .Err }}
{{ errorf "Got Err: %s. Data: %v" . .Data }}
{{ else }}
{{ errorf "Got Err: %s" . }}
{{ with .Cause }}{{ errorf "Data: %s" .Data }}{{ end }}
{{ else with .Value }}
Content: {{ .Content }}
{{ else }}
{{ errorf "Unable to get remote resource: %s" $url }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource: %s" $url }}
{{ end }}
`