错误
语法
RESOURCE.Err
返回
resource.resourceError
由 resources.GetRemote
函数返回的资源的 Err
方法,如果 HTTP 请求失败则返回错误消息,否则返回 nil。 如果您不自己处理错误,Hugo 将会构建失败。
在此示例中,我们向一个不存在的域发送 HTTP 请求
{{ $url := "https://broken-example.org/images/a.jpg" }}
{{ with resources.GetRemote $url }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
上面的代码捕获了来自 HTTP 请求的错误,然后构建失败
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
要将错误记录为警告而不是错误
{{ $url := "https://broken-example.org/images/a.jpg" }}
{{ with resources.GetRemote $url }}
{{ with .Err }}
{{ warnf "%s" . }}
{{ else }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}