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: allow callers to handle errors #52437

Open
bep opened this issue Apr 19, 2022 · 1 comment
Open

proposal: text/template: allow callers to handle errors #52437

bep opened this issue Apr 19, 2022 · 1 comment
Labels
Milestone

Comments

@bep
Copy link
Contributor

@bep bep commented Apr 19, 2022

Currently, if you do:

{{ $value := .Foo }}

And .Foo returns an error or panics, the template has no way to recover.

There are several good reasons why one might want to to handle this in the template, but mostly situations where the error situation is temporary (e.g. a remote server is failing) and some default content could be inserted.

The obvious solution would be to allow this:

{{ $value, $err := .Foo }}

Where $err would contain any error returned by or from a panic in .Foo. Any construct on the form {{ $value := .Foo }} would panic as before.

You could argue that .Foo could be rewritten to wrap any error, so you would get:

{{ $value:= .Foo }}
{{ if .Err }}
{{ else }}
   {{ .Value }}
{{ end }}

But that means that all existing APIs needs to be duplicated/wrapped – and the above does not resemble idiomatic Go code. It would also not handle runtime errors (e.g. invalid memory address or nil pointer dereference).

@bep bep added the Proposal label Apr 19, 2022
@gopherbot gopherbot added this to the Proposal milestone Apr 19, 2022
@seankhliao
Copy link
Member

@seankhliao seankhliao commented Apr 19, 2022

cc @robpike

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Proposals
Incoming
Development

No branches or pull requests

3 participants