eloc

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 11 Imported by: 2

README

eloc

Go Reference

Add a stack trace to Go's errors so that their origin can be easily located.

Documentation

Overview

Package eloc provides tools to add a stack trace to errors so that their origin can be easily located.

As described in the "Errors" section of Effective Go, it is good practice for library authors to add enough context to an error string to identify its origin. Since a library's implementation should be hidden behind its well-defined interface, library authors should not misuse eloc as an excuse for bad error messages to their users. References to the stack of a library contradict the above idea of encapsulation. Here eloc should be seen as a helpful addition during the development of the library.

This advice is also recommended to developers of executables! Here, however, some compromises may be tolerable.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At(err error) error

func Errorf

func Errorf(format string, a ...any) error

func FmtFrame

func FmtFrame(w io.Writer, err Error)

func FmtShortStack

func FmtShortStack(w io.Writer, err Error)

func FmtStack

func FmtStack(w io.Writer, err Error)

func Frame

func Frame(err error) runtime.Frame

func New

func New(text string) error

func ParseEnv

func ParseEnv(s string) error

func RecoverAs

func RecoverAs(err *error)
Example (Error)
err := testMustCall(errors.New("ERROR"))
fmt.Println(err)
Output:

ERROR @[eloc.testPanic enabled_test.go:63]
Example (ErrorStack)
ErrorFmt = FormatterFunc(FmtShortStack)
err := testMustCall(errors.New("ERROR"))
fmt.Println(err)
Output:

ERROR @[eloc.testPanic@enabled_test.go:63,68,94/run_example.go:63/example.go:44/testing.go:1927/_testmain.go:65/proc.go:267/asm_amd64.s:1650]
Example (String)
err := testMustCall("STRING")
fmt.Println(err)
Output:

STRING @[eloc.testPanic enabled_test.go:63]

func Ret

func Ret[R any](v R, err error) (R, error)

func Stack

func Stack(err error, do func(*runtime.Frame) (cont bool))

Types

type Error

type Error internal.Error

func (Error) Error

func (err Error) Error() string

func (Error) Frame

func (err Error) Frame() runtime.Frame

func (Error) Is

func (e Error) Is(err error) bool

func (Error) Stack

func (err Error) Stack(do func(*runtime.Frame) (cont bool))

func (Error) Unwrap

func (e Error) Unwrap() error

type Formatter

type Formatter interface {
	Format(w io.Writer, err Error)
}
var (
	// Disabled allows to disable/enable all error location handling through
	// eloc. If this runtime switch is not enough one can also use the Go build
	// tag 'eloc_off' to remove eloc location handling during comiple time. In
	// that case setting Disabled has o effect.
	Disbaled atomic.Bool

	ErrorFmt Formatter = FormatterFunc(FmtShortStack)
)

type FormatterFunc

type FormatterFunc func(io.Writer, Error)

func (FormatterFunc) Format

func (ff FormatterFunc) Format(w io.Writer, err Error)

type ShortStack

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

func (*ShortStack) Collect

func (s *ShortStack) Collect(f *runtime.Frame) bool

func (*ShortStack) String

func (s *ShortStack) String() string

Directories

Path Synopsis
Package must is an experiment to see if a concise way to work with panics will break Go's convention to explicitly check for errors as described in https://blog.golang.org/error-handling-and-go.
Package must is an experiment to see if a concise way to work with panics will break Go's convention to explicitly check for errors as described in https://blog.golang.org/error-handling-and-go.

Jump to

Keyboard shortcuts

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