Question about error on fetching remote data file

Test site GitHub - kijana2204/data-test

ERROR Rebuild failed: process: ".../data-test/content/data/_content.gotmpl:14:15" at <$data.features>: can’t evaluate field features in type string

I was investigating this error until I realised the remote data file was missing (404 error). Shouldn’t the error have been detected in the code block fetching the data about the missing file, rather than when trying to process its contents?

That’s a good question.

We have had that discussion before, and we landed on the decision that we don’t treat 404 (not found) as an error, but as a “no result”, so it makes these 2 logically the same:

{{ with resources.GetRemote "https://gohugo.io/foo-bar" }}{{ . }}{{ end }}
{{ with resources.Get "not-found" }}{{ . }}{{ end }}

You can certainly treat it as an error yourself:

{{ with resources.GetRemote "https://gohugo.io/foo-bar" }}{{ . }}{{{ else }}{{ errorf "Not found!" }}{ end }}
1 Like

Using try variant you will get a result object with empty .Err and .Value

which then means if .Err and .Value are empty its a 404?
which means no Value.Data.Status or so.

{{- $URL := "https://energydata.info/dataset/9a215d6f-7dce-410e-86ba-80ca8aafd250/resource/cdfb189c-ec0a-41c9-9fec-57401b2c7d29/download/power_stations.json" }}

{{ with try (resources.GetRemote $URL) }}
  {{ warnf "TRYVAL: %s" (debug.Dump .) }}
  {{- with .Err }}
    {{- errorf "%s" . }}
  {{ else with .Value }}
    {{- $data = . | transform.Unmarshal }}
  {{ else }}
    {{- errorf "OOPS: All Empty for %q" $URL }}
  {{- end }}
{{- else }}
  {{- errorf "Unable to get remote resource %q" $URL }}
{{- end -}}

Both .Err and .Value are null

results in:

WARN  TRYVAL: {
  "Value": null,
  "Err": null
}
ERROR OOPS: All Empty for "https://energydata..."
WARN  DATA: ""

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.