// v1
msg, ok := failure.MessageOf(err)
if ok { ... }
// v2
msg := failure.MessageOf(err)
if msg != "" { ... }
Wrapper
interface has been removed and replaced by Field
interface.
No more need to wrap errors by yourself. failure
provides key-value data store in the error.
// v1
type XXX any
func (x XXX) WrapError(err error) error {
return &withXXX{x, err}
}
type withXXX struct {
xxx XXX
underlying error
}
func (w *withXXX) Unwrap() errror {
return w.underlying
}
err := failure.Wrap(err, XXX(1))
// v2
type key string
const keyX key = "x"
type XXX any
func (x XXX) SetErrorField(setter FieldSetter) {
setter.Set(keyX, x)
}
err := failure.Wrap(err, XXX(1))
Since the standard library errors
has introduced Unwrap
function to enumerate error chain, failure dropped supporting that feature. Instead of it, new UnwrapFailure
function is added to unwrap an error until the error implements new failure.Failure
interface.
// v1
itr := failure.NewIterator(err)
for itr.Next() {
err := itr.Error()
// process `err`
}
// v2
for {
...
err = errors.Unwrap(err)
// process `err`
}
for {
var f failure.Failure
f, err = failure.UnwrapFailure(err)
// process `f`
}
Now you can use failure.Failure.Value()
to extract data from error.
In v2
// v1
var tracer failure.StringTracer
failure.Trace(err, &tracer)
fmt.Println(tracer)
// v2
for {
var f failure.Failure
f, err = failure.UnwrapFailure(err)
fmt.Println(f.Value(failure.KeyCode))
}