mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
resources: Replace error handling in GetRemote with try (note)
Closes #13216
This commit is contained in:
@@ -1,145 +0,0 @@
|
||||
// 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 (
|
||||
"context"
|
||||
"image"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hugio"
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
"github.com/gohugoio/hugo/media"
|
||||
"github.com/gohugoio/hugo/resources/images"
|
||||
"github.com/gohugoio/hugo/resources/images/exif"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
)
|
||||
|
||||
var (
|
||||
_ error = (*errorResource)(nil)
|
||||
// Image covers all current Resource implementations.
|
||||
_ images.ImageResource = (*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 resource.ResourceError) resource.Resource {
|
||||
return &errorResource{ResourceError: err}
|
||||
}
|
||||
|
||||
type errorResource struct {
|
||||
resource.ResourceError
|
||||
}
|
||||
|
||||
func (e *errorResource) Err() resource.ResourceError {
|
||||
return e.ResourceError
|
||||
}
|
||||
|
||||
func (e *errorResource) ReadSeekCloser() (hugio.ReadSeekCloser, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Content(context.Context) (any, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) ResourceType() string {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) MediaType() media.Type {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Permalink() string {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) RelPermalink() string {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Name() string {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Title() string {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Params() maps.Params {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Data() any {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Height() int {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Width() int {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Process(spec string) (images.ImageResource, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Crop(spec string) (images.ImageResource, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Fill(spec string) (images.ImageResource, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Fit(spec string) (images.ImageResource, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Resize(spec string) (images.ImageResource, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Filter(filters ...any) (images.ImageResource, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Exif() *exif.ExifInfo {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Colors() ([]images.Color, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) DecodeImage() (image.Image, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) Transform(...ResourceTransformation) (ResourceTransformer, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) TransformWithContext(context.Context, ...ResourceTransformation) (ResourceTransformer, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
@@ -61,10 +61,6 @@ type nopPage int
|
||||
|
||||
var noOpPathInfo = media.DefaultPathParser.Parse(files.ComponentFolderContent, "no-op.md")
|
||||
|
||||
func (p *nopPage) Err() resource.ResourceError {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *nopPage) Aliases() []string {
|
||||
return nil
|
||||
}
|
||||
|
@@ -111,10 +111,6 @@ type testPage struct {
|
||||
sectionEntries []string
|
||||
}
|
||||
|
||||
func (p *testPage) Err() resource.ResourceError {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *testPage) Aliases() []string {
|
||||
panic("testpage: not implemented")
|
||||
}
|
||||
|
@@ -224,9 +224,6 @@ type resourceCopier interface {
|
||||
|
||||
// Copy copies r to the targetPath given.
|
||||
func Copy(r resource.Resource, targetPath string) resource.Resource {
|
||||
if r.Err() != nil {
|
||||
panic(fmt.Sprintf("Resource has an .Err: %s", r.Err()))
|
||||
}
|
||||
return r.(resourceCopier).cloneTo(targetPath)
|
||||
}
|
||||
|
||||
@@ -439,10 +436,6 @@ func (l *genericResource) Content(context.Context) (any, error) {
|
||||
return hugio.ReadString(r)
|
||||
}
|
||||
|
||||
func (r *genericResource) Err() resource.ResourceError {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *genericResource) Data() any {
|
||||
return l.sd.Data
|
||||
}
|
||||
|
@@ -43,6 +43,9 @@ type OriginProvider interface {
|
||||
|
||||
// NewResourceError creates a new ResourceError.
|
||||
func NewResourceError(err error, data any) ResourceError {
|
||||
if data == nil {
|
||||
data = map[string]any{}
|
||||
}
|
||||
return &resourceError{
|
||||
error: err,
|
||||
data: data,
|
||||
@@ -65,13 +68,6 @@ type ResourceError interface {
|
||||
ResourceDataProvider
|
||||
}
|
||||
|
||||
// ErrProvider provides an Err.
|
||||
type ErrProvider interface {
|
||||
// Err returns an error if this resource is in an error state.
|
||||
// This will currently only be set for resources obtained from resources.GetRemote.
|
||||
Err() ResourceError
|
||||
}
|
||||
|
||||
// Resource represents a linkable resource, i.e. a content page, image etc.
|
||||
type Resource interface {
|
||||
ResourceWithoutMeta
|
||||
@@ -83,7 +79,6 @@ type ResourceWithoutMeta interface {
|
||||
MediaTypeProvider
|
||||
ResourceLinksProvider
|
||||
ResourceDataProvider
|
||||
ErrProvider
|
||||
}
|
||||
|
||||
type ResourceWrapper interface {
|
||||
|
@@ -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 }}
|
||||
`
|
||||
|
||||
|
@@ -192,10 +192,6 @@ func (r *resourceAdapter) Content(ctx context.Context) (any, error) {
|
||||
return r.target.Content(ctx)
|
||||
}
|
||||
|
||||
func (r *resourceAdapter) Err() resource.ResourceError {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *resourceAdapter) GetIdentity() identity.Identity {
|
||||
return identity.FirstIdentity(r.target)
|
||||
}
|
||||
|
Reference in New Issue
Block a user