Handling getJSON errors in shortcodes?

#1

I would like to make a getJSON call in a shortcode, but I don’t want failures to abort the build. I’m using 0.55.5, and based on both #5643 and #5076 I thought that a getJSON failure should not abort the build. But if, e.g., I try to fetch information about a github pull request that doesn’t exit, the build fails immediately with:

ERROR 2019/05/08 23:28:29 Failed to get JSON resource "http://api.github.com/repos/ansible/ansible/pulls/does-not-exist": Failed to retrieve remote file: Not Found                                   

Is there a way to respond to this failure (e.g., provide some alternative text) rather than having the build blow up? Given the existing issues I didn’t want to open a new one without checking to make sure I wasn’t missing something obvious.

Thanks!

#2

The error you share doesn’t show it aborting. What’s yer repo? I’ll test it out.

#3

The error you share doesn’t show it aborting.

Ah, you seem to be correct. The reason I assumed it was aborting was because in the even of the error, hugo serve only shows the error message and does not serve any site content. Using a less smart http server I see the content is in fact generated.

That’s…I guess that’s a step forward. But what I really want is the ability to respond intelligently to failed getJSON calls. Something like this:

{{try}}
{{ $data := getJSON $url }}
...do stuff with $data here...
{{except}}
Provide some default content instead.
{{end}}

Or even:

{{ $data := getJSON $url "default content if getjson fails" }}

…because even if it doesn’t actually abort the build, the practical effect of a failed getJSON call is still the same, since any pages using the failed shortcode will have missing content.