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

New(), Errorf() with Annotators #23

Open
taxio opened this issue Oct 2, 2019 · 0 comments
Open

New(), Errorf() with Annotators #23

taxio opened this issue Oct 2, 2019 · 0 comments

Comments

@taxio
Copy link

taxio commented Oct 2, 2019

Now, annotators cannot be added when generating new error.
If we want to generate new error with annotations, we have to do like following.

fail.Wrap(fail.New("hoge"), fail.WithCode(400))

So, I want you to be able to attach annotations with fail.New() and fail.Errorf(), or create another methods.

func New(text string, annotators ...Annotator) error {
	err := &Error{Err: errors.New(text)}
	withStackTrace(0)(err)
	for _, f := range annotators {
		f(err)
	}
	return err
}

func Errorf(format string, args ...interface{}) error {
	var fmtArgs []interface{}
	var annotators []Annotator
	for _, arg := range args {
		antt, ok := arg.(Annotator)
		if ok {
			annotators = append(annotators, antt)
		} else {
			fmtArgs = append(fmtArgs, arg)
		}
	}
	err := &Error{Err: fmt.Errorf(format, fmtArgs...)}
	withStackTrace(0)(err)
	for _, f := range annotators {
		f(err)
	}
	return err
}
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

No branches or pull requests

1 participant