Allow user to handle/ignore errors in resources.Get
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 9 Dec 2021 15:57:05 +0000 (16:57 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 10 Dec 2021 10:10:41 +0000 (11:10 +0100)
commite4d6ec94b5c1ea1c670f1e7127fc20ff2371cb0b
tree89b9017705262c6c19058e7d9b2d3decb508cf90
parent6260455ba73e2f2bd16b068a72e98e48b037a179
Allow user to handle/ignore errors in resources.Get

In Hugo 0.90.0 we introduced remote support in `resources.Get`.

But with remote resources comes with a higher chance of failing a build (network issues, remote server down etc.).

Before this commit we always failed the build on any unexpected error.

This commit allows the user to check for any error (and potentially fall back to a default local resource):

```htmlbars
{{ $result := resources.Get "https://gohugo.io/img/hugo-logo.png" }}
{{ with $result }}
        {{ if .Err }}
        {{/* log the error, insert a default image etc. *}}
        {{ else }}
        <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
        {{ end }}
{{ end }}
```

Note that the default behaviour is still to fail the build, but we will delay that error until you start using the `Resource`.

Fixes #9529
hugolib/page.go
hugolib/resource_chain_test.go
resources/errorResource.go [new file with mode: 0644]
resources/page/page_nop.go
resources/page/testhelpers_test.go
resources/resource.go
resources/resource/resourcetypes.go
resources/resource_factories/create/create.go
resources/transform.go
tpl/resources/resources.go