Documentation ¶
Index ¶
- func CurrentStack(skip int) string
- func FullStack(skip int) string
- func Trace(err error) error
- func Verbose(err error) string
- func WithStack(err error) string
- func Wrap(err error) error
- type Error
- func (err *Error) Code() string
- func (err *Error) Data() interface{}
- func (err *Error) Error() string
- func (err *Error) GetError() error
- func (err *Error) Message() string
- func (err *Error) SetCodeMessage(code, message string) *Error
- func (err *Error) SetData(data interface{}) *Error
- func (err *Error) SetError(e error) *Error
- func (err *Error) Stack() string
- func (err *Error) Trace() *Error
- type Stack
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CurrentStack ¶
CurrentStack return the current stack with the deepest n stack skipped.
Example ¶
stack := CurrentStack(0) re := regexp.MustCompile(`^gitee.com/go-better/dev/debug/errs.ExampleCurrentStack /.+/errs/stack_test\.go:25 \(0x[a-f0-9]+\) `) if !re.MatchString(stack) { fmt.Println(stack) }
Output:
func FullStack ¶
FullStack return the full stack with the deepest n stack skipped.
Example ¶
stack := FullStack(0) re := regexp.MustCompile(`^runtime\.Callers /.+/src/runtime/extern\.go:(\d+) \(0x[a-f0-9]+\) gitee.com/go-better/dev/debug/errs.FullStack .*/stack\.go:(\d+) \(0x[a-f0-9]+\) gitee.com/go-better/dev/debug/errs.ExampleFullStack /.+/errs/stack_test\.go:10 \(0x[a-f0-9]+\) `) if !re.MatchString(stack) { fmt.Println(stack) }
Output:
func Trace ¶
Trace return an traced error with stack. Choose "error" return type instead of "*Error", to avoid "nil pointer" become a "non nil error".
Example ¶
err := Trace(errors.New(`connection timeout`)).(*Error) fmt.Println("Error:", err.Error()) fmt.Println("Code:" + err.Code()) fmt.Println("Message:" + err.Message()) fmt.Println("Stack:", strings.HasPrefix(err.Stack(), "gitee.com/go-better/dev/debug/errs.ExampleTrace")) fmt.Println("Data:", err.Data()) fmt.Println("GetError:", err.GetError()) fmt.Println("Trace Again:", Trace(err) == err) err.SetCodeMessage("code", "message") fmt.Println("Code:", err.Code()) fmt.Println("Message:", err.Message())
Output: Error: connection timeout Code: Message: Stack: true Data: <nil> GetError: connection timeout Trace Again: true Code: code Message: message
func WithStack ¶
Example ¶
err := errors.New("the error") fmt.Println(WithStack(err)) stack := WithStack(Trace(err)) re := regexp.MustCompile(`the error gitee.com/go-better/dev/debug/errs.ExampleWithStack /.+/errs/stack_test\.go:38 \(0x[a-z0-9]+\) `) if !re.MatchString(stack) { fmt.Println(stack) }
Output: the error
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func New ¶
Example ¶
err := New(`no-login`, `please login first.`) fmt.Println("Error:", err.Error()) fmt.Println("Code:", err.Code()) fmt.Println("Message:", err.Message()) fmt.Println("Stack:" + err.Stack()) fmt.Println("Data:", err.Data()) fmt.Println("GetError:", err.GetError()) err.Trace() fmt.Println("Stack:", strings.HasPrefix(err.Stack(), "gitee.com/go-better/dev/debug/errs.ExampleNew")) err.SetData("data") fmt.Println("Data:", err.Data()) err.SetError(errors.New("the error")) fmt.Println("GetError:", err.GetError())
Output: Error: no-login: please login first. Code: no-login Message: please login first. Stack: Data: <nil> GetError: <nil> Stack: true Data: data GetError: the error
func Tracef ¶
Example ¶
err := Tracef("connection timeout: %d", 3) fmt.Println("Error:", err.Error()) fmt.Println("Code:" + err.Code()) fmt.Println("Message:" + err.Message()) fmt.Println("Stack:", strings.HasPrefix(err.Stack(), "gitee.com/go-better/dev/debug/errs.ExampleTrace")) fmt.Println("Data:", err.Data()) fmt.Println("GetError:", err.GetError()) fmt.Println("Trace Again:", Trace(err) == err)
Output: Error: connection timeout: 3 Code: Message: Stack: true Data: <nil> GetError: connection timeout: 3 Trace Again: true
func (*Error) SetCodeMessage ¶
Click to show internal directories.
Click to hide internal directories.