Skip to content
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

proposal: text/template: option for ignoring/returning errors in function calls #59868

Open
blmayer opened this issue Apr 27, 2023 · 6 comments
Labels
Milestone

Comments

@blmayer
Copy link

blmayer commented Apr 27, 2023

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!

@gopherbot gopherbot added this to the Proposal milestone Apr 27, 2023
@seankhliao 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
@seankhliao
Copy link
Member

cc @robpike

@ianlancetaylor
Copy link
Contributor

CC @robpike

@earthboundkid
Copy link
Contributor

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}}

@blmayer
Copy link
Author

blmayer commented Apr 28, 2023

I already tried that using two versions:

  1. wrap takes (string, error)
  2. wrap takes string

Both cases fail with different errors:

  1. executing "text.txt" at : wrong number of args for wrap: want 2 got 1
  2. executing "text.txt" at <.Run>: error calling Run: [error from .Run]

I also tried more exotic stuff like a variadic wrap and using call inside the templates.

@seankhliao
Copy link
Member

see #52437 for a different proposal on handling errors in templates

@blmayer
Copy link
Author

blmayer commented Apr 28, 2023

Cool, that works for me too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants