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

ExecFuncStringWithErr will not panick when f call returns error #19

Merged
merged 3 commits into from
Jan 28, 2020

Conversation

caibirdme
Copy link
Contributor

ExecFuncString will panic if f call returns an error. But panic isn't a good choice, why not just return that error to user? To avoid unexpected panic, user must recover before calling ExecFuncString, this is a hidden requirement to call ExecFuncString.

For the sake of not breaking the compatibility, I added two new method called ExecFuncStringWithErr, which will not panic when f get an error, just returns an empty string and that error directly

Copy link
Owner

@valyala valyala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also substitute function bodies for already existing ExecuteFuncString functions with the corresponding calls to ExecuteFuncStingWithErr?

template.go Outdated
@@ -285,6 +309,26 @@ func (t *Template) ExecuteFuncString(f TagFunc) string {
return s
}

// ExecuteFuncString calls f on each template tag (placeholder) occurrence
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExecuteFuncString -> ExecuteFuncStringWithErr

@caibirdme
Copy link
Contributor Author

Could you also substitute function bodies for already existing ExecuteFuncString functions with the corresponding calls to ExecuteFuncStingWithErr?

You mean making the existing ExecuteFuncString don't panic anymore?
I've already added a ExecuteFuncStingWithErr and t.ExecuteFuncStingWithErr, I think these may be enough to use

@valyala
Copy link
Owner

valyala commented Jan 27, 2020

You mean making the existing ExecuteFuncString don't panic anymore?

No, I mean calling *WithErr functions from there and panic on errors. This should reduce code duplication.

@caibirdme
Copy link
Contributor Author

You mean making the existing ExecuteFuncString don't panic anymore?

No, I mean calling *WithErr functions from there and panic on errors. This should reduce code duplication.

Oh, I get it!

@caibirdme
Copy link
Contributor Author

@valyala Done

Copy link
Owner

@valyala valyala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@valyala valyala merged commit f2edffe into valyala:master Jan 28, 2020
@valyala
Copy link
Owner

valyala commented Jan 28, 2020

@caibirdme , thanks for the contribution!

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

Successfully merging this pull request may close these issues.

2 participants