-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
text/template: panics encountered during function calls are hard to treat and debug #28242
Comments
Also updated since we get the same result for html/template too, please feel free to update accordingly. |
I agree that this shouldn't panic, just like in the other cases. Otherwise, some valid uses of |
Here is a shorter repro - the first method call isn't necessary:
Upon closer inspection, I'm no longer sure that this is a simple fix. Calling methods on nil receivers is a valid thing to do in Go, and some named types with methods have I do understand that the reported error is very confusing though, as the panic looks almost exactly like what a nil dereference bug in @bep do you specifically want an error in this case, or would a better panic message with more context be enough? I'm thinking that we have a few options:
The least invasive solution is adding context to the panic. For example, imagine:
A somewhat more friendly approach would be to return an
|
A panic is fine as long as I can get some contextual information from it (template name, line number, field/method name). The below is an example of what I get if I try to access a non-existing field: template: example.html:2:15: executing "example.html" at <.NotFound>: can't evaluate field ...` If I could get something similar as a panic in my "nil method" example, I would be a happy camper. |
There are other instances of standard library packages recovering panics and not panicking again, such as Then there is the question of whether swapping this panic with an error would be considered a breaking change. If so, we could then add a setting via /cc @robpike @rogpeppe for a decision. I'd prefer returning an error in this case, even if it means an extra template option, over returning panics with context. |
Just realised that, even if we just replace a panic with another one that contains context, we could still break existing programs. They could depend on the type of panic's argument, which would be lost if we switch the panic or replace it with an error. So I think my final proposal is to add |
This does seem very similar to fmt. It seems reasonable to catch a panic in a method and turn it into a more gracefully-returned error. |
@rsc to clarify - are you fine with changing this behavior always, or only behind a |
Change https://golang.org/cl/143097 mentions this issue: |
Change https://golang.org/cl/159998 mentions this issue: |
Updates #28242 Change-Id: Ib717b64f1f368cc889895a2437ff2943ed4eab0d Reviewed-on: https://go-review.googlesource.com/c/159998 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
Updates golang#28242 Change-Id: Ib717b64f1f368cc889895a2437ff2943ed4eab0d Reviewed-on: https://go-review.googlesource.com/c/159998 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
Updates golang#28242 Change-Id: Ib717b64f1f368cc889895a2437ff2943ed4eab0d Reviewed-on: https://go-review.googlesource.com/c/159998 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
The above panics:
I understand that the user should wrap the above in a
with
or something, but having maintained Hugo for some years I can say this:That, and if you follow the code in the Go source, there are nil checks for the other cases, but not for the "method case":
https://github.com/golang/go/blob/master/src/text/template/exec.go#L594
The text was updated successfully, but these errors were encountered: