errors

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 7 Imported by: 5

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()
    fmt.Println("Error with stack trace:")
    fmt.Println(errors.Format(err))

    stderr := fmt.Errorf("std error")
    fmt.Println("Go std error:")
    fmt.Println(errors.Format(stderr))
}

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

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

Output:

Error with stack trace:
foo failed
    ./pkg/samples.go:34
        main.foo
    ./pkg/samples.go:10
        main.main
    go1.20.2/rc/runtime/proc.go:250
        runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598
        runtime.goexit
caused by: bar failed
    ./pkg/samples.go:38
        main.bar
    ./pkg/samples.go:33
        main.foo
    ./pkg/samples.go:10
        main.main
    go1.20.2/rc/runtime/proc.go:250
        runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598
        runtime.goexit

Go std error:
std error

Verbosity levels

Errors can be formatted with different verbosity levels with:

fmt.Println(errors.Formatv(err), verbosity)

...or by changing the global verbosity level:
errors.Config.Verbosity = 5

The default verbosity level is 5.

Verbosity level samples generated with go run ./samples:

>>> Verbosity: 0
foo failed

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

>>> Verbosity: 2
foo failed
    main.foo:34
    main.main:10
    runtime.main:250
    runtime.goexit:1598
caused by: bar failed
    main.bar:38
    main.foo:33
    main.main:10
    runtime.main:250
    runtime.goexit:1598

>>> Verbosity: 3
foo failed
    ./samples.go:34
    ./samples.go:10
    go1.20.2/rc/runtime/proc.go:250
    go1.20.2/rc/runtime/asm_amd64.s:1598
caused by: bar failed
    ./samples.go:38
    ./samples.go:33
    ./samples.go:10
    go1.20.2/rc/runtime/proc.go:250
    go1.20.2/rc/runtime/asm_amd64.s:1598

>>> Verbosity: 4
foo failed
    ./samples.go:34 main.foo
    ./samples.go:10 main.main
    go1.20.2/rc/runtime/proc.go:250 runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598 runtime.goexit
caused by: bar failed
    ./samples.go:38 main.bar
    ./samples.go:33 main.foo
    ./samples.go:10 main.main
    go1.20.2/rc/runtime/proc.go:250 runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598 runtime.goexit

>>> Verbosity: 5 (DEFAULT)
foo failed
    ./samples.go:34
        main.foo
    ./samples.go:10
        main.main
    go1.20.2/rc/runtime/proc.go:250
        runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598
        runtime.goexit
caused by: bar failed
    ./samples.go:38
        main.bar
    ./samples.go:33
        main.foo
    ./samples.go:10
        main.main
    go1.20.2/rc/runtime/proc.go:250
        runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598
        runtime.goexit

>>> Verbosity: 6
foo failed
    /Users/mendlik/Development/go/go-errors/samples/samples.go:34
        main.foo
    /Users/mendlik/Development/go/go-errors/samples/samples.go:10
        main.main
    /Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/proc.go:250
        runtime.main
    /Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/asm_amd64.s:1598
        runtime.goexit
caused by: bar failed
    /Users/mendlik/Development/go/go-errors/samples/samples.go:38
        main.bar
    /Users/mendlik/Development/go/go-errors/samples/samples.go:33
        main.foo
    /Users/mendlik/Development/go/go-errors/samples/samples.go:10
        main.main
    /Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/proc.go:250
        runtime.main
    /Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/asm_amd64.s:1598
        runtime.goexit

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	As     = errors.As
	Unwrap = errors.Unwrap
)

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.

View Source
var Config struct {
	StackTraceFormatter func(err *Error, verbosity int) string
	Verbosity           int
	BasePath            string
	BaseCachePath       string
	BaseModule          string
	BaseGoSrcPath       string
	BaseGoSrcToken      string
	MaxStackDepth       int
	MaxPrintStackFrames int
	MaxPrintCauses      int
}

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 Recover added in v0.0.3

func Recover(action func()) error

Recover executes a function and turns a panic into an error.

Example:

err := errors.Recover(func() {
  somePanicingLogic()
})

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.

Example: err := err.New("error %d", 42) err.Error() == "error 42"

func Prefix added in v0.0.3

func Prefix(err error, msg string, args ...interface{}) *Error

Similiar to wrap. Creates a new error with message that prefixes wrapped errro. Supports interpolating of message parameters.

Example: wrapped := fmt.Errorf("wrapped") err := errors.Prefix(wrapped, "errored happened") err.Error() == "error happened: wrapped" err.Unwrap() == wrapped

func Wrap

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

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

Example: wrapped := fmt.Errorf("wrapped") err := errors.wrap(wrapped, "errored happened") err.Error() == "error happened" err.Unwrap() == wrapped

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