You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The proposal is to add options like error=zero, error=default in a likewise fashion as in missingkey=....
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:
the option error=default keeps the same behavior: stop execution
error=zero sets the returned value to be the zero value for that type and keeps executing
error=errorvalue sets the returned value to be the error and keeps executing
Of 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.:
// Run function returns (string, error)
{{with .Run}}
{{if .}}
no error case with error=zero or error value with error=errorvalue
run is {{.}}
{{else}}
error case, . is empty string on option `error=zero`
{{end}}
{{end}}
Thanks!
The text was updated successfully, but these errors were encountered:
seankhliao
changed the title
proposal: affected/package: Add option error=... to modify error handling behavior in template execution
proposal: text/template: option for ignoring/returning errors in function calls
Apr 27, 2023
ISTM, you should wrap your functions in something that swallows the error if you want that behavior. For example,
// Run function returns (string, error)
// wrap converts it to (string)
{{with wrap .Run}}
{{if .}}
no error case: {{.}}
{{else}}
error case
{{end}}
{{end}}
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: