-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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 try
(as in try/catch) template func
#9737
Comments
Would that be for any function or only for functions that return error codes/information (like getJson, getCSV, etc) |
It might be nicer if we could introduce a proper keyword here and make it behave more like {{ try $value, $err := "hello = \"Hello Hugo\"" | transform.Unmarshal }}
// $err is nil
{{ else }}
// $val is nil, $err is not nil
{{ end }} This ensures the value remains intact and does not have to be unwrapped in cases where you just want to swallow the error and do nothing. In other words, all the following result in the same output: {{ try $value, $err := partial "example" . }}
{{ $value }}
{{ else }}
// do nothing
{{ end }}
// catch is optional
{{ try $value := partial "example" . }}
{{ $value }}
{{ end }}
// shorthand
{{ try partial "example" . }} |
Any. |
I have created a quick prototype in #9797 I do agree that a "proper keyword" solution would be (/cc @marshall007) great, but unless someone comes up with something brilliant, I'm afraid that needs to be formulated into a proposal for the Go issue tracker. My PR is simple enough, though, is something that I would probably want: The only current testcase I have is: {{ $g := try ("hello = \"Hello Hugo\"" | transform.Unmarshal) }}
{{ with $g.Err }}
Err1: {{ . }}
{{ else }}
Value1: {{ $g.Value.hello | safeHTML }}|
{{ end }}
{{ $g := try ("hello != \"Hello Hugo\"" | transform.Unmarshal) }}
{{ with $g.Err }}
Err2: {{ . | safeHTML }}
{{ else }}
Value2: {{ $g.Value.hello | safeHTML }}|
{{ end }} Which prints: Value1: Hello Hugo|
Err2: template: index.html:7:52: executing \"index.html\" at <transform.Unmarshal>: error calling Unmarshal: unmarshal failed: toml: expected character = The above should be general enough to catch any error (e.g. also uncaught nilpointers etc.), and you get the original error (with line/col info). Would the above be ... good enough? |
@bep ah, I assumed there would be some API to add keywords/rewrites similar to how you can expose custom functions. Given that's not the case, perhaps we can avoid introducing more API surface area by leveraging the We could have This would be handy for a few reasons:
So this would look like: {{ with "hello = \"Hello Hugo\"" | resources.Unmarshal }}
{{ if .Err }}
Err1: {{ . }}
{{ else }}
Value1: {{ .Value.hello | safeHTML }}|
{{ end }} If the data is stored in a file on disk: {{ with resources.Get "assets/data.toml" }}
{{ if .Err }}
Err1: {{ . }}
{{ else }}
Value1: {{ .Value.hello | safeHTML }}|
{{ end }} |
@marshall007 what you propose is basically what we have in |
@bep that's fair but use cases outside of Consider the inverse in having |
You mean your use cases outside these. Any func/method that may fail on bad input data is a candidate. One example would be all the image processing methods/filters (I have had situations with PNG with malformed headers where I wish I could just default to a placeholder).
I want to both handle the error and also sometimes render the error (providing as much detail as possible so I can find it).
I have no idea how the above would work/solve this issue. |
I just ran into case where this would be helpful.
Except that I have no control over |
Would this be able to provide a fallback for circular loops in shortcodes, like the problem described in https://discourse.gohugo.io/t/you-may-have-a-circular-loop-in-a-shortcode-problem/ ? |
@fekete-robert yes, that should be possible. But if it's a real circular loop I would recommend to ... fix the underlying issue. @jmooring I have forgotten about this issue. #9797 should work ... |
@bep Thanks! I'd say my issue is an implicitly triggered circular loop caused by a valid use case (from me as the user's perspective) :) |
So, what I propose here is doable (I think), but we should think hard and long before we decide if and how.
I was reminded of this in
https://discourse.gohugo.io/t/catch-exception-for-transform-unmarshal-csv/37965/10
We currently have a special case for
resources.GetRemote
which, when it fails, returns a "Resource proxy" with.Err
set.My idea would be to do something like this:
The text was updated successfully, but these errors were encountered: