-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way for the user to control errors in resources.Get #9259
Comments
So even though we have |
That's the general idea, yes. |
I think being able to interpret the error is always great and that's been lacking in I'm not sure I trust
|
@regisphilibert hmm... that's probably more added complexity than I'm prepared to take on. I have thought a little, and suggest:
|
Notes on failure modes/delays... This works:
This fails relatively quickly:
This also fails relatively quickly:
This takes a really long time to fail:
|
@bep Would your suggestion apply to all methods for both global and page resources? Thinking of:
I like your suggestion. To fail quietly:
|
What if
This way the user can simply do:
If the function could return the error, the user can handle the error if desired:
|
@vanbroup I cannot wrap my head around any implementation of your suggestion. Could you somehow outline that? Most importantly how the return value from
Yea, that is an unfortunate side effect of a bug in Go see golang/go#49366 -- It's fixed in 1.17.4, so I'll created #9265 to track that fix. Let's try to get these 2 issues fixed in 0.90.1. As to the API, we also need to consider complexity/implementation cost. So, to be more specific, what I suggest is: We add a new method And, as long as this doesn't blow the performance to pieces (run benchmarks; if that doesn't work, we may have to create a wrapper), we should be able to inject a check here: hugo/tpl/tplimpl/template_funcs.go Line 109 in 3d5dbdc
And then make sure to set and return the EDIT: Scratch the reflection magic above, I suggest we just create a With all of that in place: This will work:
And this will be (mostly) backwards compatible:
As in: If We should also as part of this double check that we only return errors for real errrors (not 404). |
The But then you would still need a way to check for errors. In my example above, I stated If the user wanted to check for an error, they could instead avoid using
Why would a 404 HTTP status not be an error? The resource would not be available? |
|
I suppose this is out of the question in Go Template?
|
Yes. |
It would break the current Hugo API as widely used, but this is possible in Go templates. |
That is why I think |
I'm pretty sure that it's not possible in Go templates, see https://go.dev/play/p/FAFc7LdJcy3 |
@vanbroup re this:
But the above would be a breaking change, and I'm not sure I would call it prettier. |
hmmm, got confused by the |
I don't think we want to use reflect, what if $result would be an empty resource who implements the error? So:
I do not think this would break anything as |
So, it would be possible to implement, but it would break in the error cases (as in: Before this change the build would break on any error, after this change it would be up to the user to handle all errors. |
I'll just note that while fetching media (images, fonts, css) is an interesting use case, I believe this will become the new Now, I just want to make sure of one thing and pardon if I'm not seeing the obvious here. Could a working response (no http error) return a falsy value like an empty string. If so, wouldn't |
Agreed, this is a 'breaking' change, does this have an impact on any users? Currently it would panic and stop, the user would need to fix. Should users not only run into these types of issues during changes/development? With remote resources this is much more likely to happen and could break your site unexpectedly. When using |
What if a remote server is down when building your site on some CI server? The thing is, this is very contextual. I would say that in most situations, the current behaviour of "stop the build as fast as possible" is the best thing to do. But there are exceptions -- and if you have a fragile remote service somewhere it may make sense to just drop that content or display some default content... The thing is, I would possibly consider a breaking change like this if it was cleaner/easier/simpler/better (and preferably much) to implement: Compare these two:
The latter example is fully backwards compatible**, it's a pattern that is fairly simple to implement (no trickery with the ** Breakages in the first example: All errors must be handled by the user, |
I think we can go in the direction you suggested, it requires a bit more conditions in some scenarios but that is not a big deal. The 404 case is still an interesting one, we need to be backwards compatible but if you try to load data from a remote server and get a 404, the data will not load but you will also not see any errors because those are explicitly ignored. You could cause the build to fail using |
Fixed in e4d6ec9 |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
See https://discourse.gohugo.io/t/0-90-0-how-to-check-for-local-resource-existence/35954
In
getJSON
we have a rather intricate setup where we allow users to "ignore" HTTP errors.I'm not a big fan of this construct, but we probably need a better way to allow the users handle this themselves.
We have talked about this before, but since then we have better control of the
truth
function in Go templates, so it should be able to do something ala:I'm not totally sure if the above is a great idea or not ...
/cc @jmooring @regisphilibert etc.
The text was updated successfully, but these errors were encountered: