eloc

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 11 Imported by: 3

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 library users. References to the stack of a library contradict the above idea of encapsulation. Rather, eloc should be seen as a helpful addition during the development of the library.

This advice is also recommended to developers of executables! However, compromises may be more tolerable here.

Controlling the Stack Trace

Package eloc allows you to set how invasive the use of eloc is for a package. This can be done in the programme code via Disabled and ErrorFmt. In an application, Disabled and ErrorFmt can also be controlled via the GO_ELOC environment variable:

  • off: No stack information is added to errors

  • frame: Only the frame that called an eloc function is added to the error (FmtFrame)

  • stack: The complete stack is added to the error an printed line by line (FmtStack)

  • short_stack: The complete stack is added to the error and printed in compact inline format (FmtShortStack)

To completely opt-out from stack-enriched errors compile with the Go build tag eloc_off.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At(err error) error

At augments err with the current location if it does not yet have a location.

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

New is like errors.New but augments the error with the current location.

func ParseEnv

func ParseEnv(s string) error

func Ret

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

Ret augments err with the current location if it does not yet have a location. It returns v and augmented err.

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 no effect.
	Disabled 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
JackTT - Gopher 🇻🇳