goadapt

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2023 License: LGPL-2.1 Imports: 10 Imported by: 34

README

goadapt

The Ck/Return pattern from this package can be used to replace the common if err != nil {return err} boilerplate. See examples/ck-return.go for usage.

  • Allows shorter, more readable code.
  • Provides more descriptive error messages.
  • Includes file name and line number in error strings by default.
  • Allows optional per-error and per-function annotations.
  • Allows optional format strings for annotations.

Inspired in part by Dave Cheney's https://github.com/pkg/errors and Harri Lainio's https://github.com/lainio/err2.

This package includes several other possibly-useful functions, including Assert(), Debug(), and Pprint(), as well as a few shortcut vars for common fmt functions:

    Pl  = fmt.Println
    Pf  = fmt.Printf
    Spf = fmt.Sprintf
    Fpf = fmt.Fprintf

Docs are admittedly still thin -- see ./examples here and elsewhere on github for now.

TODO

  • add more doc comments
  • integrate examples into test cases
  • add more examples for full coverage

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Spf = fmt.Sprintf
	Fpf = fmt.Fprintf
)

Make shortcuts for common functions

View Source
var (
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
)

Allow redirecting stdio

Functions

func Assert

func Assert(cond bool, args ...interface{})

Assert takes a bool and zero or more arguments. If the bool is true, then Assert returns. If the boolean is false, then Assert panics. The panic is of type AdaptErr. The AdaptErr contains the filename and line number of the caller. The first argument is used as a Sprintf() format string. Any remaining arguments are provided to the Sprintf() as values. The Sprintf() result is stored as AdaptErr.msg, to be used later in the AdaptErr.Error() string.

func Ck

func Ck(err error, args ...interface{})

func Debug added in v0.0.13

func Debug(msg interface{}, args ...interface{})

func ErrnoIf added in v0.0.9

func ErrnoIf(cond bool, errno syscall.Errno, args ...interface{})

ErrnoIf takes a bool and zero or more arguments. If the bool is false, then ErrnoIf returns. If the boolean is true, then ErrnoIf panics. The panic is of type AdaptErr. The AdaptErr contains the filename and line number of the caller. The first argument must be of type syscall.Errno; the AdaptErr wraps the errno. The next argument is used as a Sprintf() format string. Any remaining arguments are provided to the Sprintf() as values. The Sprintf() result is stored as AdaptErr.msg, to be used later in the AdaptErr.Error() string.

func ExitIf added in v0.0.5

func ExitIf(err, target error, args ...interface{})

func FormatArgs added in v0.0.14

func FormatArgs(args ...interface{}) (msg string)

func Halt added in v0.0.8

func Halt(rc *int, msg *string)

convert panic into returned rc and msg

func Info

func Info(msg interface{}, args ...interface{})

func Pf added in v0.0.13

func Pf(format string, args ...interface{}) (n int, err error)

Pf is a shortcut for fmt.Printf(), but it also allows a caller to redirect stdout by first setting goadapt.Stdout to the desired io.Writer.

func Pl added in v0.0.13

func Pl(args ...interface{}) (n int, err error)

Pl is a shortcut for fmt.Println(), but it also allows a caller to redirect stdout by first setting goadapt.Stdout to the desired io.Writer.

func Pprint added in v0.0.14

func Pprint(in interface{})

func Return

func Return(err *error, args ...interface{})

convert panic into returned err see https://github.com/lainio/err2 and https://blog.golang.org/go1.13-errors

func ReturnChan added in v0.0.10

func ReturnChan(errc chan error, args ...interface{})

convert panic into returned err on channel

func SetStdio added in v0.7.0

func SetStdio(stdin io.Reader, stdout, stderr io.Writer)

SetStdio allows a caller to redirect stdin, stdout, and stderr.

func Spprint added in v0.0.14

func Spprint(in interface{}) string

func Tassert added in v0.0.13

func Tassert(t *testing.T, cond bool, args ...interface{})

func Uerr

func Uerr(msg interface{}, args ...interface{})

func Unpanic added in v0.0.11

func Unpanic(errno *syscall.Errno, logfunc func(msg string))

Unpanic converts a panic into a syscall.Errno and a message.

Types

type AdaptErr

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

XXX deprecate AdaptErr in favor of Wrap and stackTracer from https://pkg.go.dev/github.com/pkg/errors

func (AdaptErr) Error

func (e AdaptErr) Error() string

func (AdaptErr) Msg

func (e AdaptErr) Msg() string

Msg uses UnWrap() to recurse through the err stack, concatenating all of the messages found in the stack and returning the result as a string. This function can be used instead of .Error() to get a shorter, cleaner message string that doesn't include file and line numbers.

func (AdaptErr) Unwrap

func (e AdaptErr) Unwrap() error

Jump to

Keyboard shortcuts

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