Skip to content

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

@blmayer

Description

@blmayer

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions