HUGO

Is it possible to set a fallback for getJSON failures?

Some Background:

Basically we’re using forestry.io with a client. For a specific content types we allow them to input a Vimeo URL. When the site is built we use getJSON to pull from the Vimeo API to get video thumbnails, embed codes, ect ect.

However sometimes content editors will paste an incorrect URL Other times they might change privacy settings for the video such making a video private or giving it a private link. (This is the issue we’ve run into most often)

Whenever either of these happens getJSON no longer can fetch the url preventing builds from finishing (both production builds and the Forestry preview server).

What I’m Trying To Accomplish

I’d like it if I could create a fallback for when getJSON fails. Then just display a simple error message in place of the video thumbnail and video player.

It seems to me that getJSON doesn’t have any way to handle errors it just causes the build to fail. It would be nice if we had something similar to the catch() method used with Javascript Promises.

Is something like this possible with Hugo? I’ve searched the documentation, but couldn’t seem to find anything.

No… unfortunately this is not possible if a remote JSON is 404 then that is a build breaking error.

There have been discussions about this in the past and current behavior is unlikely to change.

However I think that your request about the need to provide some kind of hook for an error message is legit.

So I am tagging a few people who might be interested to participate in this discussion.

cc: @bep @kaushalmodi @regis

Thanks for copying me. I am still certainly interested in not breaking the Hugo build because of a JSON fetch fail.

2 Likes

Ideating: I use getJSON and sometimes don’t have anything to pull in. I adjusted my templates to use with, and I have else statements to show it not loading.

Yes but that’s when your getJSON succeeds and no data is found right?

There was an issue for getJSON to silently fail and return false in case of failure, thus allowing users to test getJSON before doing anything. The commit is here. But it was reversed I think and a new issue proposes it being revived (https://github.com/gohugoio/hugo/issues/5766).

That’s my memory and a few repo search. Maybe other folks will have a more detailed input.

2 Likes