Handling getJSON errors in shortcodes?

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!

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

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.

1 Like

@larsks I am getting this same problem and it’s really breaking my ability to work normally - I can’t get the hugo server to start because of these errors, and I need it.

The irony is that the GetJSON error I am getting is only because I am running local, I know it won’t be a problem on the live site, the required authentication will work from there. But I need to check my work locally before I publish it!

Do you know of any way to get the hugo server to run, despite getJSON errors?

You could wrap the getJSON in a conditional to check for environment. I load different stuff between “production” (hugo) and “development” (hugo server).

Thanks - my local problem solved itself. Not 100% sure how, but the error I am getting is non-authenticated API hourly limits being hit, so maybe I just spilled into the next hour and/or caching spared a bunch of hits for me.

I will use your suggestion the next time I am stuck.

I would very much like the underlying problem to be solved - we need a way to query API’s and be able to recognize, within Hugo, the difference between the return values and be able to react to them with different content generation responses, or with interrupting the build.

Basically the issue discussed on the two Github Issues linked in the first post here, and in this one, still open.

You are hitting hourly limits on public APIs. Sounds to me that you should be using authentication.

You are not supposed to do that on a public API.

The discussion about this has taken place on GitHub and here in the past.

The rationale of the lead developer is that In the end a missing JSON is an error and as such it should break the build.

This will not be fixed as shown in the second linked GitHub issue in the first post above.

There is really no point for this topic.

1 Like