-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: text/template: export error type for missingkey #57801
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
Comments
cc @robpike |
Perhaps something like: type MissingKeyError struct {
Key string
}
func (e MissingKeyError) Error() string {
return "no entry for key"
} |
@icholy , great idea! I've updated the proposal. |
It looks like this might already exist (except for write errors): https://github.com/golang/go/blob/master/src/text/template/exec.go#L118-L124 |
Can you show an example on programmatically detecting whether the cause for an |
There is currently not an easy way to test for missing key error from
text/template
in the case that that the template was constructed with themissingkey=error
option.I propose adding a new error type in
text/template
testable viaerrors.As
and the missing key can be recovered:This would be added in a way that doesn't cause the existing error's string representation to change, allowing any users that might be checking for certain sub-string in the error strings to gracefully transition to the new way.
Edit: changed
fmt.Errorf
toerrors.New
Edit: took suggestion from @icholy to have the error be a struct type, allowing users to be able to recover the offending key.
The text was updated successfully, but these errors were encountered: