Make resource.Get return nil on 404 not found
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 9 Dec 2021 15:16:35 +0000 (16:16 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 10 Dec 2021 10:10:41 +0000 (11:10 +0100)
This is in line with the interface declaration and also how local lookups work.

Fixes #9267

hugolib/resource_chain_test.go
resources/resource_factories/create/create.go

index 05e8a9d009979161744e4a69c6940703d529dacf..efd28a95d75f86517e7c65bab5e5ddd5ab53f877 100644 (file)
@@ -413,11 +413,15 @@ CSS integrity Data first: {{ $cssFingerprinted1.Data.Integrity }} {{ $cssFingerp
 CSS integrity Data last:  {{ $cssFingerprinted2.RelPermalink }} {{ $cssFingerprinted2.Data.Integrity }}
 
 {{ $rimg := resources.Get "%[1]s/sunset.jpg" }}
+{{ $remotenotfound := resources.Get "%[1]s/notfound.jpg" }}
+{{ $localnotfound := resources.Get "images/notfound.jpg" }}
 {{ $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 }}
 
 `, ts.URL))
 
@@ -448,6 +452,9 @@ CSS integrity Data last:  /styles2.min.1cfc52986836405d37f9998a63fd6dd8608e8c410
 
 SUNSET REMOTE: sunset_%[1]s.jpg|/sunset_%[1]s.a9bf1d944e19c0f382e0d8f51de690f7d0bc8fa97390c4242a86c3e5c0737e71.jpg|900|90587
 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
+
 
 `, helpers.HashString(ts.URL+"/sunset.jpg", map[string]interface{}{})))
 
index 2616af83e550b0e42061aa41e328952c6866aeae..dc03568acbe7e9d65729d367e4f478e7e030c100 100644 (file)
@@ -35,6 +35,7 @@ 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"
@@ -154,6 +155,7 @@ 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)
@@ -186,8 +188,10 @@ func (c *Client) FromRemote(uri string, options map[string]interface{}) (resourc
                        return nil, err
                }
 
-               if res.StatusCode < 200 || res.StatusCode > 299 {
-                       return nil, errors.Errorf("failed to retrieve remote resource: %s", http.StatusText(res.StatusCode))
+               if res.StatusCode != http.StatusNotFound {
+                       if res.StatusCode < 200 || res.StatusCode > 299 {
+                               return nil, errors.Errorf("failed to retrieve remote resource: %s", http.StatusText(res.StatusCode))
+                       }
                }
 
                httpResponse, err := httputil.DumpResponse(res, true)
@@ -207,6 +211,11 @@ func (c *Client) FromRemote(uri string, options map[string]interface{}) (resourc
                return nil, err
        }
 
+       if res.StatusCode == http.StatusNotFound {
+               // Not found. This matches how looksup for local resources work.
+               return nil, nil
+       }
+
        body, err := ioutil.ReadAll(res.Body)
        if err != nil {
                return nil, errors.Wrapf(err, "failed to read remote resource %s", uri)