gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
73.68k stars 7.4k forks source link

Make the error message for "ERROR failed to fetch remote resource: Forbidden" more useful #12518

Open darix opened 1 month ago

darix commented 1 month ago

At least including the uri in the error message would help the user to figure out which resources they need to fix.

Locally I applied this patch to get some ideas what we have to fix as Twitter gave us some 403.

diff --git a/resources/resource_factories/create/remote.go b/resources/resource_factories/create/remote.go
index c2d17e7a5..9424fc5a0 100644
--- a/resources/resource_factories/create/remote.go
+++ b/resources/resource_factories/create/remote.go
@@ -138,7 +138,7 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou

                if res.StatusCode != http.StatusNotFound {
                    if res.StatusCode < 200 || res.StatusCode > 299 {
-                       return nil, temporaryHTTPStatusCodes[res.StatusCode], toHTTPError(fmt.Errorf("failed to fetch remote resource: %s", http.StatusText(res.StatusCode)), res, !isHeadMethod)
+                       return nil, temporaryHTTPStatusCodes[res.StatusCode], toHTTPError(fmt.Errorf("failed to fetch remote resource '%s': %s", uri, http.StatusText(res.StatusCode)), res, !isHeadMethod)
                    }
                }
darix commented 1 month ago

alternatively the error could bubble up to the caller location to make it easier to find the line location that caused this.

Encountered error "Forbidden" while fetching tweet ...
darix commented 1 month ago

if we would let it bubble up it would also give the possibility to say "oh if rendering this failed i ignore it" and make the render a noop

darix commented 1 month ago

updated patch for 0.127.0

diff --git a/resources/resource_factories/create/remote.go b/resources/resource_factories/create/remote.go
index 5f4879dfe..d1cc9ede1 100644
--- a/resources/resource_factories/create/remote.go
+++ b/resources/resource_factories/create/remote.go
@@ -212,7 +212,7 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
        }

        if res.StatusCode < 200 || res.StatusCode > 299 {
-           return nil, toHTTPError(fmt.Errorf("failed to fetch remote resource: %s", http.StatusText(res.StatusCode)), res, !isHeadMethod)
+           return nil, toHTTPError(fmt.Errorf("failed to fetch remote resource '%s': %s", uri, http.StatusText(res.StatusCode)), res, !isHeadMethod)
        }

        var (