summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-errors/v2
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-05-08 19:49:45 +0200
committerLibravatar GitHub <noreply@github.com>2022-05-08 18:49:45 +0100
commit5004e0a9da665ccc0e18cd4075ee636641b71f0a (patch)
treeb7c8269b954ced61afa9fffd7305bd88acca6f8e /vendor/codeberg.org/gruf/go-errors/v2
parent[bugfix] Fix existing bio text showing as HTML (#531) (diff)
downloadgotosocial-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')
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/LICENSE9
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/README.md5
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/callers.go102
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/error.go35
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/error_notrace.go33
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/errors.go36
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/once.go47
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/standard.go99
8 files changed, 366 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/LICENSE b/vendor/codeberg.org/gruf/go-errors/v2/LICENSE
new file mode 100644
index 000000000..e4163ae35
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/LICENSE
@@ -0,0 +1,9 @@
+MIT License
+
+Copyright (c) 2022 gruf
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/README.md b/vendor/codeberg.org/gruf/go-errors/v2/README.md
new file mode 100644
index 000000000..90049abec
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/README.md
@@ -0,0 +1,5 @@
+# go-errors
+
+simple but powerful errors library that allows easy wrapping and stacktracing of errors.
+
+to disable stacktraces set the `notrace` build tag. \ No newline at end of file
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/callers.go b/vendor/codeberg.org/gruf/go-errors/v2/callers.go
new file mode 100644
index 000000000..77a2c1c1b
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/callers.go
@@ -0,0 +1,102 @@
+package errors
+
+import (
+ "encoding/json"
+ "runtime"
+ "strconv"
+ "strings"
+ "unsafe"
+)
+
+// Callers is a stacktrace of caller PCs.
+type Callers []uintptr
+
+// GetCallers returns a Callers slice of PCs, of at most 'depth'.
+func GetCallers(skip int, depth int) Callers {
+ rpc := make([]uintptr, depth)
+ n := runtime.Callers(skip+1, rpc)
+ return Callers(rpc[0:n])
+}
+
+// Frames fetches runtime frames for a slice of caller PCs.
+func (f Callers) Frames() []runtime.Frame {
+ // Allocate expected frames slice
+ frames := make([]runtime.Frame, 0, len(f))
+
+ // Get frames iterator for PCs
+ iter := runtime.CallersFrames(f)
+
+ for {
+ // Get next frame in iter
+ frame, ok := iter.Next()
+ if !ok {
+ break
+ }
+
+ // Append to frames slice
+ frames = append(frames, frame)
+ }
+
+ return frames
+}
+
+// MarshalJSON implements json.Marshaler to provide an easy, simply default.
+func (f Callers) MarshalJSON() ([]byte, error) {
+ // JSON-able frame type
+ type frame struct {
+ Func string `json:"func"`
+ File string `json:"file"`
+ Line int `json:"line"`
+ }
+
+ // Allocate expected frames slice
+ frames := make([]frame, 0, len(f))
+
+ // Get frames iterator for PCs
+ iter := runtime.CallersFrames(f)
+
+ for {
+ // Get next frame
+ f, ok := iter.Next()
+ if !ok {
+ break
+ }
+
+ // Append to frames slice
+ frames = append(frames, frame{
+ Func: funcname(f.Function),
+ File: f.File,
+ Line: f.Line,
+ })
+ }
+
+ // marshal converted frames
+ return json.Marshal(frames)
+}
+
+// String will return a simple string representation of receiving Callers slice.
+func (f Callers) String() string {
+ // Guess-timate to reduce allocs
+ buf := make([]byte, 0, 64*len(f))
+
+ // Convert to frames
+ frames := f.Frames()
+
+ for i := 0; i < len(frames); i++ {
+ frame := frames[i]
+
+ // Append formatted caller info
+ funcname := funcname(frame.Function)
+ buf = append(buf, funcname+"()\n\t"+frame.File+":"...)
+ buf = strconv.AppendInt(buf, int64(frame.Line), 10)
+ buf = append(buf, '\n')
+ }
+
+ return *(*string)(unsafe.Pointer(&buf))
+}
+
+// funcname splits a function name with pkg from its path prefix.
+func funcname(name string) string {
+ i := strings.LastIndex(name, "/")
+ return name[i+1:]
+}
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/error.go b/vendor/codeberg.org/gruf/go-errors/v2/error.go
new file mode 100644
index 000000000..ed1217a29
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/error.go
@@ -0,0 +1,35 @@
+//go:build !notrace
+// +build !notrace
+
+package errors
+
+type errormsg struct {
+ msg string
+ wrap error
+ stack Callers
+}
+
+func create(msg string, wrap error) *errormsg {
+ return &errormsg{
+ msg: msg,
+ wrap: wrap,
+ stack: GetCallers(2, 10),
+ }
+}
+
+func (err *errormsg) Error() string {
+ return err.msg
+}
+
+func (err *errormsg) Is(target error) bool {
+ other, ok := target.(*errormsg)
+ return ok && (err.msg == other.msg)
+}
+
+func (err *errormsg) Unwrap() error {
+ return err.wrap
+}
+
+func (err *errormsg) Stacktrace() Callers {
+ return err.stack
+}
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/error_notrace.go b/vendor/codeberg.org/gruf/go-errors/v2/error_notrace.go
new file mode 100644
index 000000000..e5faf80a2
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/error_notrace.go
@@ -0,0 +1,33 @@
+//go:build notrace
+// +build notrace
+
+package errors
+
+type errormsg struct {
+ msg string
+ wrap error
+}
+
+func create(msg string, wrap error) *errormsg {
+ return &errormsg{
+ msg: msg,
+ wrap: wrap,
+ }
+}
+
+func (err *errormsg) Error() string {
+ return err.msg
+}
+
+func (err *errormsg) Is(target error) bool {
+ other, ok := target.(*errormsg)
+ return ok && (err.msg == other.msg)
+}
+
+func (err *errormsg) Unwrap() error {
+ return err.wrap
+}
+
+func (err *errormsg) Stacktrace() Callers {
+ return nil
+}
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/errors.go b/vendor/codeberg.org/gruf/go-errors/v2/errors.go
new file mode 100644
index 000000000..180fc6799
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/errors.go
@@ -0,0 +1,36 @@
+package errors
+
+import (
+ "fmt"
+)
+
+// New returns a new error created from message.
+func New(msg string) error {
+ return create(msg, nil)
+}
+
+// Newf returns a new error created from message format and args.
+func Newf(msgf string, args ...interface{}) error {
+ return create(fmt.Sprintf(msgf, args...), nil)
+}
+
+// Wrap will wrap supplied error within a new error created from message.
+func Wrap(err error, msg string) error {
+ return create(msg, err)
+}
+
+// Wrapf will wrap supplied error within a new error created from message format and args.
+func Wrapf(err error, msgf string, args ...interface{}) error {
+ return create(fmt.Sprintf(msgf, args...), err)
+}
+
+// Stacktrace fetches a stored stacktrace of callers from an error, or returns nil.
+func Stacktrace(err error) Callers {
+ var callers Callers
+ if err, ok := err.(interface { //nolint
+ Stacktrace() Callers
+ }); ok {
+ callers = err.Stacktrace()
+ }
+ return callers
+}
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/once.go b/vendor/codeberg.org/gruf/go-errors/v2/once.go
new file mode 100644
index 000000000..83a45a61f
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/once.go
@@ -0,0 +1,47 @@
+package errors
+
+import (
+ "sync/atomic"
+ "unsafe"
+)
+
+// OnceError is an error structure that supports safe multi
+// threaded usage and setting only once (until reset).
+type OnceError struct{ err unsafe.Pointer }
+
+// NewOnce returns a new OnceError instance.
+func NewOnce() OnceError {
+ return OnceError{
+ err: nil,
+ }
+}
+
+// Store will safely set the OnceError to value, no-op if nil.
+func (e *OnceError) Store(err error) {
+ // Nothing to do
+ if err == nil {
+ return
+ }
+
+ // Only set if not already
+ atomic.CompareAndSwapPointer(
+ &e.err,
+ nil,
+ unsafe.Pointer(&err),
+ )
+}
+
+// Load will load the currently stored error.
+func (e *OnceError) Load() error {
+ return *(*error)(atomic.LoadPointer(&e.err))
+}
+
+// IsSet returns whether OnceError has been set.
+func (e *OnceError) IsSet() bool {
+ return (atomic.LoadPointer(&e.err) != nil)
+}
+
+// Reset will reset the OnceError value.
+func (e *OnceError) Reset() {
+ atomic.StorePointer(&e.err, nil)
+}
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 }