Skip to content

Error wrappers #6

@achille-roussel

Description

@achille-roussel

(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 like

type Wrapper interface {
  Wrap(error) error
}

Such wrappers would be composable with a function like

func With(err error, wrappers ...Wrapper) error

Which would apply wrappers to an error and return the final result. Using the function would look like

errors.With(err,
  errors.Message("hello"),
  errors.Types("A", "B"),
  errors.Tags(...),
  ...
)

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 an Error interface type which implements both error and methods to chain the wrap operations, for example:

type Error interface {
  error
  WithMessage(msg string) Error
  WithTypes(types ...string) Error
  WithTags(tags ...Tag) Error
  ...
}

which would be used as

errors.Wrap(err, "hello").
  WithTypes(...).
  WithTags(...)

May be best to experiment with one approach first, or provide both in the first place.

Feedback are welcome!

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions