diff options
author | 2022-05-08 19:49:45 +0200 | |
---|---|---|
committer | 2022-05-08 18:49:45 +0100 | |
commit | 5004e0a9da665ccc0e18cd4075ee636641b71f0a (patch) | |
tree | b7c8269b954ced61afa9fffd7305bd88acca6f8e /vendor/codeberg.org/gruf/go-errors/v2/standard.go | |
parent | [bugfix] Fix existing bio text showing as HTML (#531) (diff) | |
download | gotosocial-5004e0a9da665ccc0e18cd4075ee636641b71f0a.tar.xz |
[bugfix] Fix remote media pruning failing if media already gone (#548)
* fix error check of prune to allow missing files
* update go-store library, add test for pruning item with db entry but no file
Signed-off-by: kim <grufwub@gmail.com>
* remove now-unneccessary error check
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-errors/v2/standard.go')
-rw-r--r-- | vendor/codeberg.org/gruf/go-errors/v2/standard.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/standard.go b/vendor/codeberg.org/gruf/go-errors/v2/standard.go new file mode 100644 index 000000000..225d9e0c1 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-errors/v2/standard.go @@ -0,0 +1,99 @@ +package errors + +import ( + "errors" + "reflect" + + "codeberg.org/gruf/go-bitutil" +) + +// Is reports whether any error in err's chain matches any of targets +// (up to a max of 64 targets). +// +// The chain consists of err itself followed by the sequence of errors obtained by +// repeatedly calling Unwrap. +// +// An error is considered to match a target if it is equal to that target or if +// it implements a method Is(error) bool such that Is(target) returns true. +func Is(err error, targets ...error) bool { + var flags bitutil.Flags64 + + if len(targets) > 64 { + panic("too many targets") + } + + // Determine if each of targets are comparable + for i := 0; i < len(targets); { + // Drop nil errors + if targets[i] == nil { + targets = append(targets[:i], targets[i+1:]...) + continue + } + + // Check if this error is directly comparable + if reflect.TypeOf(targets[i]).Comparable() { + flags = flags.Set(uint8(i)) + } + + i++ + } + + for err != nil { + var errorIs func(error) bool + + // Check if this layer supports .Is interface + is, ok := err.(interface{ Is(error) bool }) + if ok { + errorIs = is.Is + } else { + errorIs = neveris + } + + for i := 0; i < len(targets); i++ { + // Try directly compare errors + if flags.Get(uint8(i)) && + err == targets[i] { + return true + } + + // Try use .Is() interface + if errorIs(targets[i]) { + return true + } + } + + // Unwrap to next layer + err = errors.Unwrap(err) + } + + return false +} + +// As finds the first error in err's chain that matches target, and if one is found, sets +// target to that error value and returns true. Otherwise, it returns false. +// +// The chain consists of err itself followed by the sequence of errors obtained by +// repeatedly calling Unwrap. +// +// An error matches target if the error's concrete value is assignable to the value +// pointed to by target, or if the error has a method As(interface{}) bool such that +// As(target) returns true. In the latter case, the As method is responsible for +// setting target. +// +// An error type might provide an As method so it can be treated as if it were a +// different error type. +// +// As panics if target is not a non-nil pointer to either a type that implements +// error, or to any interface type. +func As(err error, target interface{}) bool { + return errors.As(err, target) +} + +// Unwrap returns the result of calling the Unwrap method on err, if err's +// type contains an Unwrap method returning error. Otherwise, Unwrap returns nil. +func Unwrap(err error) error { + return errors.Unwrap(err) +} + +// neveris fits the .Is(error) bool interface function always returning false. +func neveris(error) bool { return false } |