-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: text/template: option for ignoring/returning errors in function calls #59868
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
Comments
error=...
to modify error handling behavior in template execution
cc @robpike |
CC @robpike |
ISTM, you should wrap your functions in something that swallows the error if you want that behavior. For example,
|
I already tried that using two versions:
Both cases fail with different errors:
I also tried more exotic stuff like a variadic wrap and using call inside the templates. |
see #52437 for a different proposal on handling errors in templates |
Cool, that works for me too. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The proposal is to add options like
error=zero
,error=default
in a likewise fashion as inmissingkey=...
.The intention is to let users specify how function calls inside templates should be handled. In a template pipeline with functions (or methods) calls with 2 returns, a value and an error, execution stops if the error is not nil. I think it would be useful if the user could specify how to proceed. So passing options seems to be a natural way. For example:
error=default
keeps the same behavior: stop executionerror=zero
sets the returned value to be the zero value for that type and keeps executingerror=errorvalue
sets the returned value to be the error and keeps executingOf course the key name could be different I just used
error
here to clarify. Implementing this would make error handling in templates possible, thus making it much more powerful. E.g.:Thanks!
The text was updated successfully, but these errors were encountered: