Err.md - hugo - [fork] hugo port for 9front
(HTM) git clone https://git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
Err.md (2017B)
---
1 ---
2 title: Err
3 description: Applicable to resources returned by the resources.GetRemote function, returns an error message if the HTTP request fails, else nil.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: resource.resourceError
9 signatures: [RESOURCE.Err]
10 expiryDate: 2027-01-16 # deprecated 2025-01-16 in v0.141.0
11 ---
12
13 {{< deprecated-in 0.141.0 >}}
14 Use the `try` statement instead. See [example].
15
16 [example]: /functions/go-template/try/#example
17 {{< /deprecated-in >}}
18
19 The `Err` method on a resource returned by the [`resources.GetRemote`] function returns an error message if the HTTP request fails, else nil. If you do not handle the error yourself, Hugo will fail the build.
20
21 [`resources.GetRemote`]: /functions/resources/getremote/
22
23 In this example we send an HTTP request to a nonexistent domain:
24
25 ```go-html-template
26 {{ $url := "https://broken-example.org/images/a.jpg" }}
27 {{ with resources.GetRemote $url }}
28 {{ with .Err }}
29 {{ errorf "%s" . }}
30 {{ else }}
31 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
32 {{ end }}
33 {{ else }}
34 {{ errorf "Unable to get remote resource %q" $url }}
35 {{ end }}
36 ```
37
38 The code above captures the error from the HTTP request, then fails the build:
39
40 ```text
41 ERROR error calling resources.GetRemote: Get "https://broken-example.org/images/a.jpg": dial tcp: lookup broken-example.org on 127.0.0.53:53: no such host
42 ```
43
44 To log an error as a warning instead of an error:
45
46 ```go-html-template
47 {{ $url := "https://broken-example.org/images/a.jpg" }}
48 {{ with resources.GetRemote $url }}
49 {{ with .Err }}
50 {{ warnf "%s" . }}
51 {{ else }}
52 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
53 {{ end }}
54 {{ else }}
55 {{ errorf "Unable to get remote resource %q" $url }}
56 {{ end }}
57 ```
58
59 > [!note]
60 > An HTTP response with a 404 status code is not an HTTP request error. To handle 404 status codes, code defensively using the nested `with-else-end` construct as shown above.