mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
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
This commit is contained in:
parent
6260455ba7
commit
e4d6ec94b5
10 changed files with 185 additions and 23 deletions
|
@ -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 {
|
||||
|
|
|
@ -415,6 +415,7 @@ 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 }}
|
||||
|
@ -422,6 +423,8 @@ SUNSET REMOTE: {{ $rimg.Name }}|{{ $rimg.RelPermalink }}|{{ $rimg.Width }}|{{ le
|
|||
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))
|
||||
|
||||
|
@ -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{}{})))
|
||||
|
|
129
resources/errorResource.go
Normal file
129
resources/errorResource.go
Normal file
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -30,7 +30,6 @@ import (
|
|||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
|
||||
|
||||
"github.com/gohugoio/hugo/navigation"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hugo"
|
||||
|
@ -121,6 +120,10 @@ type testPage struct {
|
|||
sectionEntries []string
|
||||
}
|
||||
|
||||
func (p *testPage) Err() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *testPage) Aliases() []string {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -45,6 +45,7 @@ type Resource interface {
|
|||
ResourceMetaProvider
|
||||
ResourceParamsProvider
|
||||
ResourceDataProvider
|
||||
Err() error
|
||||
}
|
||||
|
||||
// Image represents an image resource.
|
||||
|
|
|
@ -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"
|
||||
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
return ns.createClient.FromRemote(filenamestr, options)
|
||||
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")
|
||||
}
|
||||
return ns.createClient.FromRemote(filenamestr, nil)
|
||||
|
||||
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
|
||||
}
|
||||
return ns.createClient.FromRemote(filenamestr, options)
|
||||
}
|
||||
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.
|
||||
|
|
Loading…
Reference in a new issue