hugo: Failures in getJSON crash the build instead of allowing the user to handle it.

As suggested in #5643 I am creating a new issue for this.

getJSON causes builds to fail and halt in the versions 0.68.3, 0.69.1 and 0.69.2 which I have tested it on. This happens when accessing a remote file at a HTTP address (have not tested for local json files, not my use case).

The use case I have is that we’re checking a database through its REST API for the existence of an entry to pull some metadata, and falling back to local data if not present. However, the only way we can check for the existence is to make a query, and if it’s not there, the getJSON errors, and the build halts.

Specific error is this: Failed to get JSON resource "https://data.rcsb.org/rest/v1/core/entry/": Failed to retrieve remote file: Not Found.

Supposedly #5648 was meant to fix this, but from what I’m reading never did it properly?

I don’t know Go so I’m not sure exactly how to fix it, but I think I can trace its stack:

I have a couple ideas on what behavior I would like to see to make this more user and use-case friendly.

  1. Is it possible to make the getRemote on HTTPError not actually throw the error and instead log it, pass it back up and let the calling function handle it? Especially since getRemote isn’t a publicly available function in Hugo’s shortcodes?
  2. Could there be some switch to tell the getRemote to pass it back up (like if there is another function invoking which could handle it?)
  3. Could the getRemote or getJson throw a warning instead? This does leave it up to the site developer to properly handle the case when the JSON they get back isn’t what they expect, but I think that’s a reasonable thing to ask for.
  4. Use a with...else...end construct to handle the return from getJSON when it errors. This actually already works as expected. I can test it with the shortcode construct of
    {{ with getJson "...." }}
        <!-- Do stuff on success -->
    {{ else }}
        {{ warnf "It failed" }}
    {{ end }}
    
    This will correctly throw not only the getJSON error, but also throw the warnf warning in the console. However, the fact that an error was thrown causes the built to fail in the end, but the getJSON error clearly is not fatal to the overall build code.
  5. Have a function to probe an external site? Basically a function to see if the url exists. I think getRemote does this already to some extent, but make this return a boolean instead of the result or error?

What version of Hugo are you using (hugo version)?

0.68.3, 0.69.1 and 0.69.2 (currently using)

Sepcifically: Hugo Static Site Generator v0.69.2/extended darwin/amd64 installed via brew

Does this issue reproduce with the latest release?

Yes: 0.69.2

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

@onedrawingperday @bep @maelle

I just did a local build with the current master and can confirm that #7867 does fix the problem and the with...else...end construct works correctly.

For anyone looking for this solution in the future for some version after 0.76.5

  • Add ignoreErrors = ["error-remote-getjson"] to your config file
  • Trap getJSON calls you expect to fail with the following
    {{ with getJson "...." }}
        <!-- Do stuff on success -->
    {{ else }}
        <!-- Do stuff on failure -->
    {{ end }}
    

Thanks to everyone who replied and thanks to @bep for implementing a fix.

@maelle

With the above in a project’s config the console will show a WARN for a missing getJSON remote. It’s up to you what you’re going to do with it. The project build will no longer be halted.

That’s great to hear! I’d need to test it more thoroughly to make sure it works correctly.

@maelle when I originally made the issue, you can establish a fallback with a with...else block such as this:

{{ with getJson "...." }}
    <!-- Do stuff on success -->
{{ else }}
    {{ warnf "It failed" }}
{{ end }}

which would fail since the getJson still raised an error.

@onedrawingperday do you know if a warning raised by getJSON will still trigger the else block of that construct, or would it be considered success enough and run the with block?

Related to this (and partly repeating ideas from above, sorry), would it be possible to have

  • an argument to getJSON/getRemote (and maybe even built-in shortcodes) that’d allow to give a warning instead of an error with a default behavior to error – and maybe even a fallback option;

  • a hugo build/server option to change that error/warning option site-wide. (so for instance the build could be less strict in branches, more strict on master, depending on a Netlify config)

Use case

  • Being able to ignore that a tweet/instagram post disappeared when working on something else in the website. see #6954

It’s great to get some message that a resource couldn’t be fetched, to e.g. remove deleted tweets IDs from content, but sometimes it halts work on other issues (e.g. I’m editing a post, and the build will fail because a tweet ID in another post is obsolete).