errors

package module
v0.0.1 Latest Latest
Warning

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

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

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.Printf("\n>>> Format %%s:\n%s", err)
	fmt.Printf("\n>>> Format %%v:\n%v", err)
	fmt.Printf("\n>>> Format %%+v:\n%+v", err)
	fmt.Printf("\n>>> Format %%#v:\n%#v", err)
}

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 %s:
foo failed

>>> Format %v:
foo failed
	main.foo:19
	main.main:10
	runtime.main:250
	runtime.goexit:1598
caused by: bar failed
	main.bar:23
	main.foo:18
	main.main:10
	runtime.main:250
	runtime.goexit:1598

>>> Format %+v:
foo failed
	./samples.go:19
		main.foo
	./samples.go:10
		main.main
	<GO_SRC_DIR>/runtime/proc.go:250
		runtime.main
	<GO_SRC_DIR>/runtime/asm_amd64.s:1598
		runtime.goexit
caused by: bar failed
	./samples.go:23
		main.bar
	./samples.go:18
		main.foo
	./samples.go:10
		main.main
	<GO_SRC_DIR>/runtime/proc.go:250
		runtime.main
	<GO_SRC_DIR>/runtime/asm_amd64.s:1598
		runtime.goexit

>>> Format %#v:
foo failed
	<PROJECT_DIR>/samples/samples.go:19
		main.foo
	<PROJECT_DIR>/samples/samples.go:10
		main.main
	<GO_SRC_DIR>/runtime/proc.go:250
		runtime.main
	<GO_SRC_DIR>/runtime/asm_amd64.s:1598
		runtime.goexit
caused by: bar failed
	<PROJECT_DIR>/samples/samples.go:23
		main.bar
	<PROJECT_DIR>/samples/samples.go:18
		main.foo
	/Users/mendlik/Development/go/go-errors/samples/samples.go:10
		main.main
	<GO_SRC_DIR>/runtime/proc.go:250
		runtime.main
	<GO_SRC_DIR>/runtime/asm_amd64.s:1598
		runtime.goexit

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	As                  = errors.As
	Unwrap              = errors.Unwrap
	BasePath            = ""
	BaseCachePath       = ""
	BaseModule          = ""
	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 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) Format

func (e *Error) Format(s fmt.State, verb rune)

func (*Error) StackTrace

func (e *Error) StackTrace() StackTrace

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

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file relative to the compile time GOPATH
%d    source line
%n    function name without package

Format accepts flags that alter the printing of some verbs, as follows:

%+s   full source file path
%+n   function name with 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