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 the missingkey=error option.
I propose adding a new error type in text/template testable via errors.As and the missing key can be recovered:
type MissingKeyError struct {
Key string
}
func (e MissingKeyError) Error() string {
return "no entry for key"
}
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 to errors.New
Edit: took suggestion from @icholy to have the error be a struct type, allowing users to be able to recover the offending key.
There is currently not an easy way to test for missing key error from
text/templatein the case that that the template was constructed with themissingkey=erroroption.I propose adding a new error type in
text/templatetestable viaerrors.Asand 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.Errorftoerrors.NewEdit: took suggestion from @icholy to have the error be a struct type, allowing users to be able to recover the offending key.