errs

package
v0.0.0-...-d31700d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrentStack

func CurrentStack(skip int) string

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

func FullStack(skip int) string

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

func Trace(err error) error

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 Verbose

func Verbose(err error) string

Verbose print err verbosely

func WithStack

func WithStack(err error) string
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

func Wrap

func Wrap(err error) error

Wrap return an wrapped error. Choose "error" return type instead of "*Error", to avoid "nil pointer" become a "non nil error".

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func New

func New(code, message string) *Error
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 Newf

func Newf(code, message string, args ...interface{}) *Error

func Tracef

func Tracef(format string, args ...interface{}) *Error
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) Code

func (err *Error) Code() string

func (*Error) Data

func (err *Error) Data() interface{}

func (*Error) Error

func (err *Error) Error() string

func (*Error) GetError

func (err *Error) GetError() error

func (*Error) Message

func (err *Error) Message() string

func (*Error) SetCodeMessage

func (err *Error) SetCodeMessage(code, message string) *Error

func (*Error) SetData

func (err *Error) SetData(data interface{}) *Error

func (*Error) SetError

func (err *Error) SetError(e error) *Error

func (*Error) Stack

func (err *Error) Stack() string

func (*Error) Trace

func (err *Error) Trace() *Error

type Stack

type Stack struct {
	Skip int
}

func (*Stack) IncrSkip

func (s *Stack) IncrSkip() *Stack

func (*Stack) String

func (s *Stack) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL