How to handle getJSON errors?

I’m getting a bunch of JSON feeds using the getJSON function. However, not all those feeds exist.

Is there anyway to catch this error and ignore it?

Template changed /Users/..../partials/mytemplate.html
ERROR 2017/09/12 15:44:28 Failed to get json resource https:...
1 Like

Hi,

any answer to this ?

getJSON is supposed to return nil when an error occures, we’ve tried to test our var against nil in the following way but still hugo crash on with an error on getJSON

ERROR 2018/05/23 18:12:13 Error while rendering “page” in “ville/”: template:…/single.html:144:11: executing “main” at <partial "block-local…>: error calling partial: template: theme/partials/block-local-.html:64:21: executing “theme/partials/block-local.html” at <getJSON ($.Scratch.G…>: error calling getJSON: Get http://127.0.0.1//test.md: EOF

In our template
{{ dataJSON := (getJSON (.Scratch.Get “baseurlapi”) $element) }}
{{ if ne $dataJSON nil }}

{{ end }}
{{ end }}

looks like errors are catched in hugolib/site.go, we would need that error to be silent and not killing hugo, any advice to achieve that ?

That would be indeed very interesting

I’ve opened an issue to see what bep has to say. Let’s continue further discussion there:

Since fdfa4a5 the following can be entered in a project’s config to allow building a project even if a remote source called by getJSON is missing.

If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site config:
ignoreErrors = ["error-remote-getjson"]

P.S. This option will be available in the next Hugo release.

3 Likes

2 years later

Wow! I am still using Hugo, but I believe I haven’t updated it in about that much time. Now’s a good reason, at least for me, to update it on the next release :crossed_fingers: :smiley:

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