Skip to content

Commit

Permalink
Allow user to handle/ignore errors in resources.Get
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bep committed Dec 10, 2021
1 parent 6260455 commit e4d6ec9
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 20 deletions.
4 changes: 4 additions & 0 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ type pageState struct {
*pageCommon
}

func (p *pageState) Err() error {
return nil
}

// Eq returns whether the current page equals the given page.
// This is what's invoked when doing `{{ if eq $page $otherPage }}`
func (p *pageState) Eq(other interface{}) bool {
Expand Down
5 changes: 5 additions & 0 deletions hugolib/resource_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,16 @@ CSS integrity Data last: {{ $cssFingerprinted2.RelPermalink }} {{ $cssFingerpri
{{ $rimg := resources.Get "%[1]s/sunset.jpg" }}
{{ $remotenotfound := resources.Get "%[1]s/notfound.jpg" }}
{{ $localnotfound := resources.Get "images/notfound.jpg" }}
{{ $gopherprotocol := resources.Get "gopher://example.org" }}
{{ $rfit := $rimg.Fit "200x200" }}
{{ $rfit2 := $rfit.Fit "100x200" }}
{{ $rimg = $rimg | fingerprint }}
SUNSET REMOTE: {{ $rimg.Name }}|{{ $rimg.RelPermalink }}|{{ $rimg.Width }}|{{ len $rimg.Content }}
FIT REMOTE: {{ $rfit.Name }}|{{ $rfit.RelPermalink }}|{{ $rfit.Width }}
REMOTE NOT FOUND: {{ if $remotenotfound }}FAILED{{ else}}OK{{ end }}
LOCAL NOT FOUND: {{ if $localnotfound }}FAILED{{ else}}OK{{ end }}
PRINT PROTOCOL ERROR1: {{ with $gopherprotocol }}{{ . | safeHTML }}{{ end }}
PRINT PROTOCOL ERROR2: {{ with $gopherprotocol }}{{ .Err | safeHTML }}{{ end }}
`, ts.URL))

Expand Down Expand Up @@ -454,6 +457,8 @@ SUNSET REMOTE: sunset_%[1]s.jpg|/sunset_%[1]s.a9bf1d944e19c0f382e0d8f51de690f7d0
FIT REMOTE: sunset_%[1]s.jpg|/sunset_%[1]s_hu59e56ffff1bc1d8d122b1403d34e039f_0_200x200_fit_q75_box.jpg|200
REMOTE NOT FOUND: OK
LOCAL NOT FOUND: OK
PRINT PROTOCOL ERROR1: error calling resources.Get: Get "gopher://example.org": unsupported protocol scheme "gopher"
PRINT PROTOCOL ERROR2: error calling resources.Get: Get "gopher://example.org": unsupported protocol scheme "gopher"
`, helpers.HashString(ts.URL+"/sunset.jpg", map[string]interface{}{})))
Expand Down
129 changes: 129 additions & 0 deletions resources/errorResource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2021 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package resources

import (
"image"

"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/media"

"github.com/gohugoio/hugo/resources/images/exif"

"github.com/gohugoio/hugo/resources/resource"
)

var (
_ error = (*errorResource)(nil)
// Imnage covers all current Resource implementations.
_ resource.Image = (*errorResource)(nil)
// The list of user facing and exported interfaces in resource.go
// Note that if we're missing some interface here, the user will still
// get an error, but not as pretty.
_ resource.ContentResource = (*errorResource)(nil)
_ resource.ReadSeekCloserResource = (*errorResource)(nil)
_ resource.ResourcesLanguageMerger = (*resource.Resources)(nil)
// Make sure it also fails when passed to a pipe function.
_ ResourceTransformer = (*errorResource)(nil)
)

// NewErrorResource wraps err in a Resource where all but the Err method will panic.
func NewErrorResource(err error) resource.Resource {
return &errorResource{error: err}
}

type errorResource struct {
error
}

func (e *errorResource) Err() error {
return e.error
}

func (e *errorResource) ReadSeekCloser() (hugio.ReadSeekCloser, error) {
panic(e.error)
}

func (e *errorResource) Content() (interface{}, error) {
panic(e.error)
}

func (e *errorResource) ResourceType() string {
panic(e.error)
}

func (e *errorResource) MediaType() media.Type {
panic(e.error)
}

func (e *errorResource) Permalink() string {
panic(e.error)
}

func (e *errorResource) RelPermalink() string {
panic(e.error)
}

func (e *errorResource) Name() string {
panic(e.error)
}

func (e *errorResource) Title() string {
panic(e.error)
}

func (e *errorResource) Params() maps.Params {
panic(e.error)
}

func (e *errorResource) Data() interface{} {
panic(e.error)
}

func (e *errorResource) Height() int {
panic(e.error)
}

func (e *errorResource) Width() int {
panic(e.error)
}

func (e *errorResource) Fill(spec string) (resource.Image, error) {
panic(e.error)
}

func (e *errorResource) Fit(spec string) (resource.Image, error) {
panic(e.error)
}

func (e *errorResource) Resize(spec string) (resource.Image, error) {
panic(e.error)
}

func (e *errorResource) Filter(filters ...interface{}) (resource.Image, error) {
panic(e.error)
}

func (e *errorResource) Exif() *exif.Exif {
panic(e.error)
}

func (e *errorResource) DecodeImage() (image.Image, error) {
panic(e.error)
}

func (e *errorResource) Transform(...ResourceTransformation) (ResourceTransformer, error) {
panic(e.error)
}
4 changes: 4 additions & 0 deletions resources/page/page_nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var (
// PageNop implements Page, but does nothing.
type nopPage int

func (p *nopPage) Err() error {
return nil
}

func (p *nopPage) Aliases() []string {
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion resources/page/testhelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/bep/gitmap"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/resources/resource"


"github.com/gohugoio/hugo/navigation"

Expand Down Expand Up @@ -121,6 +120,10 @@ type testPage struct {
sectionEntries []string
}

func (p *testPage) Err() error {
return nil
}

func (p *testPage) Aliases() []string {
panic("not implemented")
}
Expand Down
4 changes: 4 additions & 0 deletions resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func (l *genericResource) Content() (interface{}, error) {
return l.content, nil
}

func (r *genericResource) Err() error {
return nil
}

func (l *genericResource) Data() interface{} {
return l.data
}
Expand Down
1 change: 1 addition & 0 deletions resources/resource/resourcetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Resource interface {
ResourceMetaProvider
ResourceParamsProvider
ResourceDataProvider
Err() error
}

// Image represents an image resource.
Expand Down
2 changes: 0 additions & 2 deletions resources/resource_factories/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/gohugoio/hugo/hugofs"

"github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types"
Expand Down Expand Up @@ -155,7 +154,6 @@ func (c *Client) FromString(targetPath, content string) (resource.Resource, erro
// FromRemote expects one or n-parts of a URL to a resource
// If you provide multiple parts they will be joined together to the final URL.
func (c *Client) FromRemote(uri string, options map[string]interface{}) (resource.Resource, error) {
defer herrors.Recover()
rURL, err := url.Parse(uri)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse URL for resource %s", uri)
Expand Down
4 changes: 4 additions & 0 deletions resources/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func (r *resourceAdapter) Content() (interface{}, error) {
return r.target.Content()
}

func (r *resourceAdapter) Err() error {
return nil
}

func (r *resourceAdapter) Data() interface{} {
r.init(false, false)
return r.target.Data()
Expand Down
44 changes: 27 additions & 17 deletions tpl/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,40 @@ func (ns *Namespace) getscssClientDartSass() (*dartsass.Client, error) {
// further transformations.
//
// For URLs an additional argument with options can be provided.
func (ns *Namespace) Get(args ...interface{}) (resource.Resource, error) {
if len(args) != 1 && len(args) != 2 {
return nil, errors.New("must provide a filename or URL")
}
func (ns *Namespace) Get(args ...interface{}) resource.Resource {
get := func(args ...interface{}) (resource.Resource, error) {
if len(args) != 1 && len(args) != 2 {
return nil, errors.New("must provide a filename or URL")
}

filenamestr, err := cast.ToStringE(args[0])
if err != nil {
return nil, err
}
filenamestr, err := cast.ToStringE(args[0])
if err != nil {
return nil, err
}

if u, err := url.Parse(filenamestr); err == nil && u.Scheme != "" {
if len(args) == 2 {
options, err := maps.ToStringMapE(args[1])
if err != nil {
return nil, err
if u, err := url.Parse(filenamestr); err == nil && u.Scheme != "" {
if len(args) == 2 {
options, err := maps.ToStringMapE(args[1])
if err != nil {
return nil, err
}
return ns.createClient.FromRemote(filenamestr, options)
}
return ns.createClient.FromRemote(filenamestr, options)
return ns.createClient.FromRemote(filenamestr, nil)
}
return ns.createClient.FromRemote(filenamestr, nil)

filenamestr = filepath.Clean(filenamestr)

return ns.createClient.Get(filenamestr)
}

filenamestr = filepath.Clean(filenamestr)
r, err := get(args...)
if err != nil {
// This allows the client to reason about the .Err in the template.
return resources.NewErrorResource(errors.Wrap(err, "error calling resources.Get"))
}
return r

return ns.createClient.Get(filenamestr)
}

// GetMatch finds the first Resource matching the given pattern, or nil if none found.
Expand Down

0 comments on commit e4d6ec9

Please sign in to comment.