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

Pass ...Wrapper list to New constructor #31

Closed
Southclaws opened this issue Jun 5, 2023 · 0 comments · Fixed by #42
Closed

Pass ...Wrapper list to New constructor #31

Southclaws opened this issue Jun 5, 2023 · 0 comments · Fixed by #42

Comments

@Southclaws
Copy link
Owner

Southclaws commented Jun 5, 2023

A pattern I've seen pop up in usage of Fault is constructing a new error then immediately wrapping it.

image

For example, this is from a real codebase, we mostly use sentinel errors for fault.News but it still pops up from time to time.

return nil, fault.Wrap(fault.New("user account not found"),
	fctx.With(ctx),
	ftag.With(ftag.NotFound),
	fmsg.WithDesc("not found",
		"No account could be found with the specified email address."),

Two problems:

Ideal interface:

return nil, fault.New("user account not found",
	fctx.With(ctx),
	ftag.With(ftag.NotFound),
	fmsg.With("No account could be found with the specified email address."),

Mainly what I'd like to tackle is giving fault.New a new signature:

func New(message string, w ...Wrapper) error

And immediately perform the wrapping internally if w contains any elements. This would allow you to create new errors and immediately pass them through your wrappers of choice without the need to first create the error then pass it to Wrap().

It would also make the wrapped sentinel pattern a bit less verbose too (a pattern for declaring sentinel errors with pre-determined wrappers such as error tags)

// Before
var (
    ErrEmptyPassword     = fault.Wrap(fault.New("password parameter is empty"), ftag.With(ftag.InvalidArgument))
    ErrUserNotFound      = fault.Wrap(fault.New("user account not found"), ftag.With(ftag.NotFound))
)

// After
var (
    ErrEmptyPassword     = fault.New("password parameter is empty", ftag.With(ftag.InvalidArgument))
    ErrUserNotFound      = fault.New("user account not found", ftag.With(ftag.NotFound))
)
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 a pull request may close this issue.

1 participant