Documentation
¶
Overview ¶
Package erf provides error management with stack trace.
Index ¶
- Variables
- func Errorf(format string, a ...interface{}) error
- func PC(size, skip int) []uintptr
- func Wrap(err error) error
- func Wrapp(perr *error)
- type Erf
- func (e *Erf) Arg(index int) interface{}
- func (e *Erf) Args() []interface{}
- func (e *Erf) Attach(tags ...string) *Erf
- func (e *Erf) Attach2(tags ...string) error
- func (e *Erf) Copy() *Erf
- func (e *Erf) CopyByTop(top int) *Erf
- func (e *Erf) Error() string
- func (e *Erf) Fmt() string
- func (e *Erf) Format(f fmt.State, verb rune)
- func (e *Erf) Len() int
- func (e *Erf) PC() []uintptr
- func (e *Erf) PCLen() int
- func (e *Erf) StackTrace() *StackTrace
- func (e *Erf) Tag(tag string) interface{}
- func (e *Erf) TagIndex(tag string) int
- func (e *Erf) Tags() []string
- func (e *Erf) TagsLen() int
- func (e *Erf) Unwrap() error
- func (e *Erf) UnwrapAll() []error
- type StackCaller
- type StackTrace
- type WrappedError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultPCSize defines max length of Erf program counters in PC() method. DefaultPCSize = int(uintptr(os.Getpagesize()) / unsafe.Sizeof(uintptr(0))) )
Functions ¶
func Errorf ¶
Errorf is similar with Newf except that it returns the error interface instead of the Erf pointer.
Types ¶
type Erf ¶
type Erf struct {
// contains filtered or unexported fields
}
Erf is an error type that wraps the underlying error that stores and formats the stack trace.
Example ¶
package main import ( "fmt" "github.com/goinsane/erf" ) func main() { e := erf.New("an example erf error") err := erf.Errorf("we have an example error: %w", e) fmt.Println("just show the first error message without padding and indent.") fmt.Printf("%v\n\n", err) fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '%+s'.") fmt.Printf("%x\n\n", err) fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '% s'.") fmt.Printf("% x\n\n", err) fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '%#s'.") fmt.Printf("%#x\n\n", err) fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '% #s'.") fmt.Printf("% #x\n\n", err) fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '%+s'.") fmt.Printf("%X\n\n", err) fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '% s'.") fmt.Printf("% X\n\n", err) fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '%#s'.") fmt.Printf("%#X\n\n", err) fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '% #s'.") fmt.Printf("% #X\n\n", err) fmt.Println("don't show any error messages, just show all of StackTrace of errors.") fmt.Printf("%-x\n\n", err) fmt.Println("don't show the error message, just show the StackTrace of the first error.") fmt.Printf("%-X\n\n", err) fmt.Println("padding 2, indent 1 by default. padding char '\\t'.") fmt.Printf("%2x\n\n", err) fmt.Println("padding 2, indent 3. padding char '\\t'.") fmt.Printf("%2.3x\n\n", err) fmt.Println("padding 0 by default, indent 3. padding char '\\t'.") fmt.Printf("%.3x\n\n", err) fmt.Println("padding 2, indent 2 by default. padding char ' '.") fmt.Printf("% 2x\n\n", err) fmt.Println("padding 2, indent 3. padding char ' '.") fmt.Printf("% 2.3x\n\n", err) fmt.Println("padding 0 by default, indent 3. padding char ' '.") fmt.Printf("% .3x\n\n", err) }
Output:
func Newf ¶
Newf creates a new Erf object with the given format and args. It panics if an any arg is nil.
func (*Erf) Arg ¶
Arg returns an argument value on the given index. It panics if index is out of range.
func (*Erf) Args ¶
func (e *Erf) Args() []interface{}
Args returns all argument values. It returns nil if Erf didn't create with formatting functions.
func (*Erf) Attach ¶
Attach attaches tags to arguments, if arguments are given. If tag is "", it passes attaching tag to corresponding argument. It panics for given errors:
tags are already attached number of tags is more than args tag already defined
func (*Erf) Attach2 ¶ added in v1.2.0
Attach2 is similar with Attach except that it returns the error interface instead of the Erf pointer.
func (*Erf) CopyByTop ¶ added in v1.3.0
CopyByTop creates a shallow copy of Erf that copies program counters downward from the argument top. If the argument top is out of range, it panics.
func (*Erf) Fmt ¶
Fmt returns the format argument of the formatting functions (Newf, Errorf or Wrap) that created Erf.
func (*Erf) Format ¶
Format is implementation of fmt.Formatter. Format lists error messages and appends StackTrace's for underlying Erf and all of wrapped Erf's, line by line with given format.
For '%v' (also '%s'):
%v just show the first error message without padding and indent.
For '%x' and '%X':
%x list all error messages by using indent and show StackTrace of errors by using format '%+s'. %+x similar with '%x', also shows tags. % x list all error messages by using indent and show StackTrace of errors by using format '% s'. %#x list all error messages by using indent and show StackTrace of errors by using format '%#s'. % #x list all error messages by using indent and show StackTrace of errors by using format '% #s'. %X show the first error message by using indent and show the StackTrace of error by using format '%+s'. %+X similar with '%X', also shows tags. % X show the first error message by using indent and show the StackTrace of error by using format '% s'. %#X show the first error message by using indent and show the StackTrace of error by using format '%#s'. % #X show the first error message by using indent and show the StackTrace of error by using format '% #s'. %-x don't show any error messages, just show all of StackTrace of errors. %-X don't show the error message, just show the StackTrace of the first error. %4x same with '%x', padding 4, indent 1 by default. %.3x same with '%x', padding 0 by default, indent 3. %4.3x same with '%x', padding 4, indent 3. %4.x same with '%x', padding 4, indent 0. % 4x same with '% x', padding 4, indent 2 by default. % .3x same with '% x', padding 0 by default, indent 3. % 4.3x same with '% x', padding 4, indent 3. % 4.x same with '% x', padding 4, indent 0. %#4.3x same with '%#x', padding 4, indent 3. % #4.3x same with '% #x', padding 4, indent 3.
func (*Erf) StackTrace ¶
func (e *Erf) StackTrace() *StackTrace
StackTrace returns a StackTrace of Erf.
func (*Erf) Tag ¶
Tag returns an argument value on the given tag. It returns nil if tag is not found.
func (*Erf) TagIndex ¶ added in v1.2.3
TagIndex returns index of an argument on the given tag. It returns -1 if tag is not found.
func (*Erf) Tags ¶ added in v1.2.0
Tags returns all tags sequentially. It returns nil if tags are not attached.
type StackCaller ¶
StackCaller stores the information of stack caller. StackCaller can format given information as string by using Format or String methods.
Example ¶
package main import ( "fmt" "github.com/goinsane/erf" ) func main() { e := erf.New("an example erf error") sc := e.StackTrace().Caller(0) fmt.Println("just show function and entry without padding and indent.") fmt.Printf("%s\n\n", sc) fmt.Println("show file path, line and pc. padding char '\\t', default padding 0, default indent 1.") fmt.Printf("%+s\n\n", sc) fmt.Println("use file name as file path.") fmt.Printf("%#s\n\n", sc) fmt.Println("padding 2, indent 1 by default.") fmt.Printf("%#2s\n\n", sc) fmt.Println("padding 2, indent 3.") fmt.Printf("%#2.3s\n\n", sc) fmt.Println("show file path, line and pc. padding char ' ', default padding 0, default indent 2.") fmt.Printf("% s\n\n", sc) fmt.Println("use file name as file path.") fmt.Printf("% #s\n\n", sc) fmt.Println("padding 2, indent 2 by default.") fmt.Printf("% #2s\n\n", sc) fmt.Println("padding 2, indent 3.") fmt.Printf("% #2.3s\n\n", sc) }
Output:
func (StackCaller) Format ¶
func (c StackCaller) Format(f fmt.State, verb rune)
Format is implementation of fmt.Formatter.
For '%s' (also '%v'):
%s just show function and entry without padding and indent. %+s show file path, line and pc. padding char '\t', default padding 0, default indent 1. % s show file path, line and pc. padding char ' ', default padding 0, default indent 2. %+ s exact with '% s'. %#s same with '%+s', use file name as file path. %+#s exact with '%#s'. % #s same with '% s', use file name as file path. %+4s same with '%+s', padding 4, indent 1 by default. %+.3s same with '%+s', padding 0 by default, indent 3. %+4.3s same with '%+s', padding 4, indent 3. %+4.s same with '%+s', padding 4, indent 0. % 4s same with '% s', padding 4, indent 2 by default. % .3s same with '% s', padding 0 by default, indent 3. % 4.3s same with '% s', padding 4, indent 3. % 4.s same with '% s', padding 4, indent 0. %#4.3s same with '%#s', padding 4, indent 3. % #4.3s same with '% #s', padding 4, indent 3.
func (StackCaller) String ¶
func (c StackCaller) String() string
String is implementation of fmt.Stringer. It is synonym with fmt.Sprintf("%s", c).
type StackTrace ¶
type StackTrace struct {
// contains filtered or unexported fields
}
StackTrace stores the information of stack trace.
Example ¶
package main import ( "fmt" "github.com/goinsane/erf" ) func main() { e := erf.New("an example erf error") st := e.StackTrace() fmt.Println("default") fmt.Printf("%s\n\n", st) fmt.Println("show file path, line and pc. padding char '\\t', default padding 0, default indent 1.") fmt.Printf("%+s\n\n", st) fmt.Println("show file path, line and pc. padding char ' ', default padding 0, default indent 2.") fmt.Printf("% s\n\n", st) }
Output:
func NewStackTrace ¶
func NewStackTrace(pc ...uintptr) *StackTrace
NewStackTrace creates a new StackTrace object.
func (*StackTrace) Caller ¶
func (t *StackTrace) Caller(index int) StackCaller
Caller returns a StackCaller on the given index. It panics if index is out of range.
func (*StackTrace) Duplicate ¶
func (t *StackTrace) Duplicate() *StackTrace
Duplicate duplicates the StackTrace object.
func (*StackTrace) Format ¶
func (t *StackTrace) Format(f fmt.State, verb rune)
Format is implementation of fmt.Formatter. Format lists all StackCaller's in StackTrace line by line with given format.
func (*StackTrace) Len ¶
func (t *StackTrace) Len() int
Len returns the length of the length of all Caller's.
func (*StackTrace) String ¶
func (t *StackTrace) String() string
String is implementation of fmt.Stringer. It is synonym with fmt.Sprintf("%s", t).
type WrappedError ¶
WrappedError is an interface to simulate GoLang's wrapped errors.