erf

package module
v1.0.4-rc0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: BSD-3-Clause Imports: 8 Imported by: 2

README

erf

Go Reference

Package erf provides error management with stack trace. Erf is an error type that wraps the underlying error that stores and formats the stack trace. Please see godoc.

Examples

To run any example, please use the command like the following:

cd examples/
go run example1.go

Tests

To run all tests, please use the following command:

go test -v

To run all examples, please use the following command:

go test -v -run=^Example

To run all benchmarks, please use the following command:

go test -v -run=^Benchmark -bench=.

Documentation

Overview

Package erf provides error management with stack trace.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultPCSize defines max length of Erf program counters in PC() method.
	DefaultPCSize = int(4096 / unsafe.Sizeof(uintptr(0)))
)

Functions

func Errorf

func Errorf(format string, a ...interface{}) error

Errorf is similar with Newf except that it returns the error interface instead of the Erf pointer.

func PC added in v1.0.0

func PC(size, skip int) []uintptr

PC returns program counters by using runtime.Callers.

func Wrap

func Wrap(err error) error

Wrap wraps the given error as the underlying error and returns a new Erf object as the error interface. Wrap is similar with Newf("%w", err) except that it returns nil if err is nil.

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 New

func New(text string) *Erf

New creates a new Erf object with the given text.

func Newf

func Newf(format string, args ...interface{}) *Erf

Newf creates a new Erf object with the given format and args. It panics if an any arg is nil.

func (*Erf) Arg

func (e *Erf) Arg(index int) interface{}

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

func (e *Erf) Attach(tags ...string) *Erf

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) Error

func (e *Erf) Error() string

Error is implementation of error.

func (*Erf) Fmt

func (e *Erf) Fmt() string

Fmt returns the format argument of the formatting functions (Newf, Errorf or Wrap) that created Erf.

func (*Erf) Format

func (e *Erf) Format(f fmt.State, verb rune)

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) Len

func (e *Erf) Len() int

Len returns the length of the arguments slice.

func (*Erf) PC

func (e *Erf) PC() []uintptr

PC returns program counters.

func (*Erf) StackTrace

func (e *Erf) StackTrace() *StackTrace

StackTrace returns a StackTrace of Erf.

func (*Erf) Tag

func (e *Erf) Tag(tag string) interface{}

Tag returns an argument value on the given tag. It returns nil if tag is not found.

func (*Erf) Unwrap

func (e *Erf) Unwrap() error

Unwrap returns the underlying error.

type StackCaller

type StackCaller struct {
	runtime.Frame
}

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) PC

func (t *StackTrace) PC() []uintptr

PC returns program counters.

func (*StackTrace) String

func (t *StackTrace) String() string

String is implementation of fmt.Stringer. It is synonym with fmt.Sprintf("%s", t).

type WrappedError

type WrappedError interface {
	error
	Unwrap() error
}

Jump to

Keyboard shortcuts

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