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
- type Erf
- func (e *Erf) Arg(index int) interface{}
- func (e *Erf) Args() []interface{}
- func (e *Erf) Attach(tags ...string) *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) StackTrace() *StackTrace
- func (e *Erf) Tag(tag string) interface{}
- func (e *Erf) Unwrap() 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(4096 / 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") e2 := erf.Wrap(e) fmt.Println("just show first error message (default: padding char '\\t', padding 0, indent 1)") fmt.Printf("%s\n\n", e2) fmt.Println("show error message with indent, append stack trace") fmt.Printf("%+s\n\n", e2) fmt.Println("show first error message with indent, append stack trace") fmt.Printf("%+ s\n\n", e2) fmt.Println("use file name as file path for StackCaller") fmt.Printf("%+#s\n\n", e2) fmt.Println("padding 2, indent 1 by default") fmt.Printf("%+#2s\n\n", e2) fmt.Println("padding 2, indent 3") fmt.Printf("%+#2.3s\n\n", e2) fmt.Println("use ' ' as padding char (padding 0, indent 2)") fmt.Printf("%-s\n\n", e2) fmt.Println("show error message with indent, append stack trace") fmt.Printf("%-+s\n\n", e2) fmt.Println("show first error message with indent, append stack trace") fmt.Printf("%-+ s\n\n", e2) fmt.Println("use file name as file path for StackCaller") fmt.Printf("%-+#s\n\n", e2) fmt.Println("padding 2, indent 2 by default") fmt.Printf("%-+#2s\n\n", e2) fmt.Println("padding 2, indent 3") fmt.Printf("%-+#2.3s\n\n", e2) }
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. It panics for given errors:
args are not using tags are already attached number of tags is more than args tag already defined
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 all StackTrace's line by line with given format.
For '%s' (also '%v'):
%s just show first error message (default: padding char '\t', padding 0, indent 1) %+s show error message with indent, append stack trace using format '%+s' %#s use file name as file path for StackCaller %-s use ' ' as padding char (padding 0, indent 2) %0s show only first error even verb '+' was given % s exact with %0s %4s padding 4, default indent %.3s default padding, indent 3 %4.3s padding 4, indent 3 %4.s padding 4, indent 0
func (*Erf) StackTrace ¶
func (e *Erf) StackTrace() *StackTrace
StackTrace returns a StackTrace of Erf.
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 (default: padding char '\\t', padding 0, indent 1)") fmt.Printf("%s\n\n", sc) fmt.Println("show file path, line and pc") 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("use ' ' as padding char (padding 0, indent 2)") fmt.Printf("%-s\n\n", sc) fmt.Println("show file path, line and pc") 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) }
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 (default: padding char '\t', padding 0, indent 1) %+s show file path, line and pc %#s use file name as file path %-s use ' ' as padding char (padding 0, indent 2) %4s padding 4, default indent %.3s default padding, indent 3 %4.3s padding 4, indent 3 %4.s padding 4, indent 0
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") fmt.Printf("%+s\n\n", st) }
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 StackCaller's line by line with given format.
func (*StackTrace) Len ¶
func (t *StackTrace) Len() int
Len returns the length of the StackCaller slice.
func (*StackTrace) String ¶
func (t *StackTrace) String() string
String is implementation of fmt.Stringer. It is synonym with fmt.Sprintf("%s", t).