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

Add Builder feature #12

Merged
merged 2 commits into from
Oct 19, 2024
Merged

Add Builder feature #12

merged 2 commits into from
Oct 19, 2024

Conversation

m-mizutani
Copy link
Owner

This PR adds goerr.NewBuilder() to create an error with pre-defined contextual variables. It is useful when you want to create an error with the same contextual variables in multiple places.

type object struct {
	id    string
	color string
}

func (o *object) Validate() error {
	eb := goerr.NewBuilder().With("id", o.id)

	if o.color == "" {
		return eb.New("color is empty")
	}

	return nil
}

func main() {
	obj := &object{id: "object-1"}

	if err := obj.Validate(); err != nil {
		slog.Default().Error("Validation error", "err", err)
	}
}

Output:

2024/10/19 14:19:54 ERROR Validation error err.message="color is empty" err.values.id=object-1 (snip)

@m-mizutani m-mizutani merged commit 2e5ed7f into main Oct 19, 2024
5 checks passed
@m-mizutani m-mizutani deleted the feature/err-value-set branch October 19, 2024 22:29
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.

1 participant