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:
getJson
callsgetResource
and then if an error is thrown its supposed to log said error but not throw the error: https://github.com/gohugoio/hugo/blob/c2d9fd1ebef89cc19256e6799f29ab3d27eaf879/tpl/data/data.go#L118-L122getResource
attempts to callgetRemote
because the a URL was passed in, so the""
case is skipped and it goes to thedefault
: https://github.com/gohugoio/hugo/blob/c2d9fd1ebef89cc19256e6799f29ab3d27eaf879/tpl/data/resources.go#L109-L122getRemote
detects anHTTPError
and actually throws the error, which passes back up the stack: https://github.com/gohugoio/hugo/blob/c2d9fd1ebef89cc19256e6799f29ab3d27eaf879/tpl/data/resources.go#L55-L57getResource
andgetJSON
behave correctly and log the error, but the thing actually halting the build came from thegetRemote
call and theHTTPError
handling.
I have a couple ideas on what behavior I would like to see to make this more user and use-case friendly.
- Is it possible to make the
getRemote
onHTTPError
not actually throw the error and instead log it, pass it back up and let the calling function handle it? Especially sincegetRemote
isn’t a publicly available function in Hugo’s shortcodes? - 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?) - Could the
getRemote
orgetJson
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. - Use a
with...else...end
construct to handle the return fromgetJSON
when it errors. This actually already works as expected. I can test it with the shortcode construct of
This will correctly throw not only the{{ with getJson "...." }} <!-- Do stuff on success --> {{ else }} {{ warnf "It failed" }} {{ end }}
getJSON
error, but also throw thewarnf
warning in the console. However, the fact that an error was thrown causes the built to fail in the end, but thegetJSON
error clearly is not fatal to the overall build code. - 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
- https://github.com/geocompr/geocompr.github.io/runs/1347422984?check_suite_focus=true https://github.com/gohugoio/hugo/issues/7228 — committed to geocompr/geocompr.github.io by Nowosad 4 years ago
- Deploy commit: https://github.com/geocompr/geocompr.github.io/runs/1347422984?check_suite_focus=true https://github.com/gohugoio/hugo/issues/7228 74159ac94dcbf2989c69cbb6bcb2cd0d372f9ec7 — committed to geocompr/geocompr.github.io by Nowosad 4 years ago
@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
ignoreErrors = ["error-remote-getjson"]
to your config filegetJSON
calls you expect to fail with the followingThanks 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:which would fail since the
getJson
still raised an error.@onedrawingperday do you know if a warning raised by
getJSON
will still trigger theelse
block of that construct, or would it be considered success enough and run thewith
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
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).