You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A pattern I've seen pop up in usage of Fault is constructing a new error then immediately wrapping it.
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.
returnnil, 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:
fault.Wrap(fault.New is noisy and verbose
"user account not found" + "not found" is pointless duplication
returnnil, 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:
funcNew(messagestring, 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)
// Beforevar (
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))
)
// Aftervar (
ErrEmptyPassword=fault.New("password parameter is empty", ftag.With(ftag.InvalidArgument))
ErrUserNotFound=fault.New("user account not found", ftag.With(ftag.NotFound))
)
The text was updated successfully, but these errors were encountered:
A pattern I've seen pop up in usage of Fault is constructing a new error then immediately wrapping it.
For example, this is from a real codebase, we mostly use sentinel errors for
fault.New
s but it still pops up from time to time.Two problems:
fault.Wrap(fault.New
is noisy and verbosecontainer{}
wrapsIdeal interface:
Mainly what I'd like to tackle is giving
fault.New
a new signature: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)
The text was updated successfully, but these errors were encountered: