Add 'errors/' from commit 'dd131cce3d6f9c0b2351537cec85955ebc0be5e0'
Repo: github.com/lbryio/errors.go git-subtree-dir: errors git-subtree-mainline:f233e3b525
git-subtree-split:dd131cce3d
This commit is contained in:
commit
624405e3bf
5 changed files with 113 additions and 0 deletions
2
errors/.gitignore
vendored
Normal file
2
errors/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/.idea
|
||||
/vendor
|
15
errors/Gopkg.lock
generated
Normal file
15
errors/Gopkg.lock
generated
Normal file
|
@ -0,0 +1,15 @@
|
|||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
||||
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/go-errors/errors"
|
||||
packages = ["."]
|
||||
revision = "3afebba5a48dbc89b574d890b6b34d9ee10b4785"
|
||||
version = "v1.0.0"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "cdb8112723180cdbe98df0e74827b04930f26759f6c08fe39066835774c18175"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
3
errors/Gopkg.toml
Normal file
3
errors/Gopkg.toml
Normal file
|
@ -0,0 +1,3 @@
|
|||
[[constraint]]
|
||||
name = "github.com/go-errors/errors"
|
||||
version = "1.0.0"
|
4
errors/README.md
Normal file
4
errors/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
# errors
|
||||
|
||||
Better error handling. Marries [go-errors/errors](https://github.com/go-errors/errors) to [pkg/errors](https://github.com/pkg/errors), and
|
||||
adds a little bit of our own magic sauce.
|
89
errors/errors.go
Normal file
89
errors/errors.go
Normal file
|
@ -0,0 +1,89 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
base "errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
)
|
||||
|
||||
// interop with pkg/errors
|
||||
type causer interface {
|
||||
Cause() error
|
||||
}
|
||||
|
||||
// Err intelligently creates/handles errors, while preserving the stack trace.
|
||||
// It works with errors from github.com/pkg/errors too.
|
||||
func Err(err interface{}, fmtParams ...interface{}) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, ok := err.(causer); ok {
|
||||
err = fmt.Errorf("%+v", err)
|
||||
} else if errString, ok := err.(string); ok && len(fmtParams) > 0 {
|
||||
err = fmt.Errorf(errString, fmtParams...)
|
||||
}
|
||||
|
||||
return errors.Wrap(err, 1)
|
||||
}
|
||||
|
||||
// Wrap calls errors.Wrap, in case you want to skip a different amount
|
||||
func Wrap(err interface{}, skip int) *errors.Error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, ok := err.(causer); ok {
|
||||
err = fmt.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
return errors.Wrap(err, skip+1)
|
||||
}
|
||||
|
||||
// Is compares two wrapped errors to determine if the underlying errors are the same
|
||||
// It also interops with errors from pkg/errors
|
||||
func Is(e error, original error) bool {
|
||||
if c, ok := e.(causer); ok {
|
||||
e = c.Cause()
|
||||
}
|
||||
if c, ok := original.(causer); ok {
|
||||
original = c.Cause()
|
||||
}
|
||||
return errors.Is(e, original)
|
||||
}
|
||||
|
||||
// Prefix prefixes the message of the error with the given string
|
||||
func Prefix(prefix string, err interface{}) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.WrapPrefix(Err(err), prefix, 0)
|
||||
}
|
||||
|
||||
// Trace returns the stack trace
|
||||
func Trace(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
return string(Err(err).(*errors.Error).Stack())
|
||||
}
|
||||
|
||||
// FullTrace returns the error type, message, and stack trace
|
||||
func FullTrace(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
return Err(err).(*errors.Error).ErrorStack()
|
||||
}
|
||||
|
||||
// Base returns a simple error with no stack trace attached
|
||||
func Base(text string) error {
|
||||
return base.New(text)
|
||||
}
|
||||
|
||||
// HasTrace checks if error has a trace attached
|
||||
func HasTrace(err error) bool {
|
||||
_, ok := err.(*errors.Error)
|
||||
return ok
|
||||
}
|
Loading…
Reference in a new issue