gotosocial/vendor/codeberg.org/gruf/go-errors/v2/value.go
kim 68b91d2128
[performance] tweak http client error handling (#1718)
* update errors library, check for more TLS type error in http client

Signed-off-by: kim <grufwub@gmail.com>

* bump cache library version to match errors library

Signed-off-by: kim <grufwub@gmail.com>

---------

Signed-off-by: kim <grufwub@gmail.com>
2023-04-29 18:44:20 +02:00

57 lines
882 B
Go

package errors
import "errors"
// WithValue wraps err to store given key-value pair, accessible via Value() function.
func WithValue(err error, key any, value any) error {
if err == nil {
panic("nil error")
}
return &errWithValue{
err: err,
key: key,
val: value,
}
}
// Value searches for value stored under given key in error chain.
func Value(err error, key any) any {
var e *errWithValue
if !errors.As(err, &e) {
return nil
}
return e.Value(key)
}
type errWithValue struct {
err error
key any
val any
}
func (e *errWithValue) Error() string {
return e.err.Error()
}
func (e *errWithValue) Is(target error) bool {
return e.err == target
}
func (e *errWithValue) Unwrap() error {
return Unwrap(e.err)
}
func (e *errWithValue) Value(key any) any {
for {
if key == e.key {
return e.val
}
if !errors.As(e.err, &e) {
return nil
}
}
}