mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-23 10:42:12 +00:00
68b91d2128
* 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>
57 lines
882 B
Go
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
|
|
}
|
|
}
|
|
}
|