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: export error type for missingkey #57801

Open
sding3 opened this issue Jan 15, 2023 · 5 comments
Open

proposal: text/template: export error type for missingkey #57801

sding3 opened this issue Jan 15, 2023 · 5 comments
Labels
Milestone

Comments

@sding3
Copy link
Contributor

sding3 commented Jan 15, 2023

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.

@gopherbot gopherbot added this to the Proposal milestone Jan 15, 2023
@seankhliao seankhliao changed the title proposal: text/template: make it easier to discover missing key error proposal: text/template: export error value for missingkey Jan 15, 2023
@seankhliao
Copy link
Member

cc @robpike

@icholy
Copy link

icholy commented Jan 15, 2023

Perhaps something like:

type MissingKeyError struct {
    Key string
}

func (e MissingKeyError) Error() string {
    return "no entry for key"
}

@sding3
Copy link
Contributor Author

sding3 commented Jan 18, 2023

@icholy , great idea! I've updated the proposal.

@seankhliao seankhliao changed the title proposal: text/template: export error value for missingkey proposal: text/template: export error type for missingkey Jan 18, 2023
@sethvargo
Copy link
Contributor

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

@sding3
Copy link
Contributor Author

sding3 commented Jun 15, 2023

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 ExecError is due to missing keys? I don't think that capability exists today unless you resort to sub-string checks against the error string, which isn't a reliable method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants