errors

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: MIT Imports: 7 Imported by: 9

README

Coditory - Go Errors

GitHub release Go Reference Go Report Card Build Status Coverage

🚧 This library as under heavy development until release of version 1.x.x 🚧

Wrapper for Go errors that prints error causes with theis stack traces.

  • Prints stacks traces from all of the causes
  • Shortens file paths and function names for readability
  • Supports and exports errors.Is, errors.As, errors.Unwrap

Getting started

Installation

Get the dependency with:

go get github.com/coditory/go-errors

and import it in the project:

import "github.com/coditory/go-errors"

The exported package is errors, basic usage:

import "github.com/coditory/go-errors"

func main() {
	err := foo()
	// 0: error
	fmt.Printf("\n>>> Format: 0\n%s", errors.Formatv(err, 0))
	// 1: error + causes
	fmt.Printf("\n>>> Format: 1\n%s", errors.Formatv(err, 1))
	// 2: error + causes with stack traces of relative func names and lines
	fmt.Printf("\n>>> Format: 2\n%s", errors.Formatv(err, 2))
	// 3: error + causes with stack traces of relative file names and lines
	fmt.Printf("\n>>> Format: 3\n%s", errors.Formatv(err, 3))
	// 4: error + causes with stack traces of relative file names and lines
	//                        ...and relative func names
	fmt.Printf("\n>>> Format: 4\n%s", errors.Formatv(err, 4))
	// 5: like 4 but uses absolute file names and func names
	fmt.Printf("\n>>> Format: 5\n%s", errors.Formatv(err, 5))

	// standard errors are formatted with err.Error()
	goerr := fmt.Errorf("go error")
	fmt.Printf("\n>>> Format go error:\n%s", errors.Format(goerr))
}

func foo() error {
	err := bar()
	return errors.Wrap(err, "foo failed")
}

func bar() error {
	return errors.New("bar failed")
}

Output for go run ./samples

>>> Format: 0
foo failed

>>> Format: 1
foo failed
caused by: bar failed

>>> Format: 2
foo failed
	main.foo:32
	main.main:10
	runtime.main:250
	runtime.goexit:1598
caused by: bar failed
	main.bar:36
	main.foo:31
	main.main:10
	runtime.main:250
	runtime.goexit:1598

>>> Format: 3
foo failed
	./go:32
	./go:10
	go1.20.2/src/runtime/proc.go:250
	go1.20.2/src/runtime/asm_amd64.s:1598
caused by: bar failed
	./go:36
	./go:31
	./go:10
	go1.20.2/src/runtime/proc.go:250
	go1.20.2/src/runtime/asm_amd64.s:1598

>>> Format: 4
foo failed
	./go:32
		main.foo
	./go:10
		main.main
	go1.20.2/src/runtime/proc.go:250
		runtime.main
	go1.20.2/src/runtime/asm_amd64.s:1598
		runtime.goexit
caused by: bar failed
	./go:36
		main.bar
	./go:31
		main.foo
	./go:10
		main.main
	go1.20.2/src/runtime/proc.go:250
		runtime.main
	go1.20.2/src/runtime/asm_amd64.s:1598
		runtime.goexit

>>> Format: 5
foo failed
	<PROJECT_DIR>/samples/samples.go:32
		main.foo
	<PROJECT_DIR>/samples/samples.go:10
		main.main
	go1.20.2/src/runtime/proc.go:250
		runtime.main
	go1.20.2/src/runtime/asm_amd64.s:1598
		runtime.goexit
caused by: bar failed
	<PROJECT_DIR>/samples/samples.go:36
		main.bar
	<PROJECT_DIR>/samples/samples.go:31
		main.foo
	<PROJECT_DIR>/samples/samples.go:10
		main.main
	go1.20.2/src/runtime/proc.go:250
		runtime.main
	go1.20.2/src/runtime/asm_amd64.s:1598
		runtime.goexit

>>> Format go error:
go error

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	As                  = errors.As
	Unwrap              = errors.Unwrap
	Verbosity           = 2
	BasePath            = ""
	BaseCachePath       = ""
	BaseModule          = ""
	BaseGoSrcPath       = ""
	BaseGoSrcToken      = ""
	MaxStackDepth       = 32
	MaxPrintStackFrames = 5
	MaxPrintCauses      = 5
)

Export a number of functions or variables from pkg/errors. We want people to be able to use them, if only via the entrypoints we've vetted in this file.

Functions

func Format added in v0.0.2

func Format(err error) string

func Formatv added in v0.0.2

func Formatv(err error, verbosity int) string

func Is

func Is(e error, original error) bool

Detects whether the error is equal to a given error. Errors are considered equal by this function if they are matched by errors.Is or if their contained errors are matched through errors.Is

func RecoverPanic

func RecoverPanic(r interface{}, errPtr *error)

RecoverPanic turns a panic into an error.

Example:

func Do() (err error) {
  defer func() {
    errors.RecoverPanic(recover(), &err)
  }()
}

Types

type Error

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

func New

func New(msg string, args ...interface{}) *Error

Creates a new error with a stack trace. Supports interpolating of message parameters.

func Wrap

func Wrap(err error, msgAndArgs ...interface{}) *Error

Creates a new error with a cause and a stack trace.

func (*Error) Error

func (e *Error) Error() string

func (*Error) StackTrace

func (e *Error) StackTrace() StackTrace

func (*Error) StackTraceString added in v0.0.2

func (e *Error) StackTraceString(verbosity int) string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

func (Frame) File added in v0.0.2

func (f Frame) File() string

File returns the full path to the File that contains the function for this Frame's pc.

func (Frame) FuncName added in v0.0.2

func (f Frame) FuncName() string

FuncName returns the FuncName of this function, if known.

func (Frame) Line added in v0.0.2

func (f Frame) Line() int

Line returns the Line number of source code of the function for this Frame's pc.

func (Frame) Pc added in v0.0.2

func (f Frame) Pc() uintptr

Pc returns the program counter for this frame; multiple frames may have the same PC value.

func (Frame) RelFile added in v0.0.2

func (f Frame) RelFile() string

file name relateive to BasePath or BaseCachePath

func (Frame) RelFuncName added in v0.0.2

func (f Frame) RelFuncName() string

function name relative to main package

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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