-
Notifications
You must be signed in to change notification settings - Fork 5
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
Error wrappers #6
Comments
Instead of:
I personally prefer the signature:
so that you can compose them nicely with a function with a signature similar to:
Replace fmt with whatever problem specific args you need to supply. Great example of where this is used elsewhere is http middleware chaining. |
I like this idea! 👍 What's your take on func With(wrappers ...Wrapper) Wrapper vs func With(wrappers ...Wrapper) func(error) error ? |
The first solution seems to be easier to implement and maintain. The problem is that appropriate package-level names like My best shot at the alternative names is this:
Returning a richer interface from functions on the other hand would allow to use more semantic names: |
How about a naming scheme like func MessageWrapper(msg string) Wrapper { ... }
func TypesWrapper(types ...string) Wrapper { ... }
func TagsWrapper(tags ...Tag) Wrapper { ... } ? A bit verbose but naming consistency may make the API friendlier to developers. |
That's pretty semantical, too. An implementation could look like this:
|
Another tricky part here is to know how many frames
In which case it would be appropriate to skip only 2 frames. |
While I agree it’s nice to get the frame stack right, I wouldn’t worry too much about showing one too many frames. People can figure it out when reading the stack trace, and we can still solve this problem later on. |
Either work fine for me :-) Can always have
|
I know this is old but thought I'd post the solution @andreiko and I had come up with https://github.com/go-playground/errors It avoids all the wrapping and much of the complexity. |
I humbly refuse to take credit for that ☝️It was your solution @joeybloggs. |
(based on feedback from @andreiko)
We need a better way to compose or chain error wrappers, one idea that came in a discussion would be to use a
Wrapper
type which would look likeSuch wrappers would be composable with a function like
Which would apply wrappers to an error and return the final result. Using the function would look like
While this form of chaining is very flexible and allows it is also a bit verbose, the package name gets repeated over and over. Another approach could be to use the
Wrap
function as a constructor for anError
interface type which implements botherror
and methods to chain the wrap operations, for example:which would be used as
May be best to experiment with one approach first, or provide both in the first place.
Feedback are welcome!
The text was updated successfully, but these errors were encountered: