errutil

package module
v0.0.0-...-87ce252 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 4 Imported by: 90

Documentation

Overview

Package errutil extends go errors with various helpers.

Index

Constants

This section is empty.

Variables

View Source
var Fprintf = fmt.Fprintf

Fprintf acts like its package fmt counterpart. See errutil.Sprintf for notses.

View Source
var Panic = false

Panic when true triggers on any new errutil error... unless it implements the NoPanic interface

View Source
var Sprintf = fmt.Sprintf

Sprintf acts like its package fmt counterpart. It removes a direct dependency on fmt to make usages other uses of fmt easier to find. It may eventually change to use a lighterweight formatting, ex. maybe using pkg/internal/reflectlite ( for %T handling. )

Functions

func Append

func Append(err error, errs ...error) error

Append joins two errors into one; either or both can be nil.

func Fmt

func Fmt(format string, parts ...any) error

Fmt maps to fmt.Errorf()

func New

func New(parts ...any) (err error)

New captures the passed arguments to build an error. Wraps the last element if it's an error. The returned error delays evaluating the parts until `Error() string`, Relies on the presumption that, in an error state, the rest of the program wont be altering pending values anyway. If that's not true, then Sprint or Sprintf are better options.

func Prefix

func Prefix(err error, s string) error

Prefix the passed error with the passed string, returning a brand new error.

func PrintErrors

func PrintErrors(err error, w func(s string))

PrintErrors is a utility function that prints a list of errors to w, one error per line, if the err parameter is a MultiError. Otherwise it prints the err string. w is not a writer b/c log.Writer() doesnt generate log headers automatically and note log.Println and fmt.Println have different signatures, so yay. we use the simplest form possible here.

func Sprint

func Sprint(parts ...any) string

Sprint fixes the fact that package fmt Sprint doesnt add spaces if the parts themselves are strings or if the parts implement Stringer. When printing errors, who knows what the types implement; we want the error formatting to look good regardless.

Types

type Error

type Error string

Error type which uses a single string as an error. Can also be used to create error constants. https://dave.cheney.net/2016/04/07/constant-errors ex. const MyError = errutil.Error("github.com/a/package/MyError")

func (Error) Error

func (e Error) Error() string

func (Error) Is

func (e Error) Is(target error) bool

type Func

type Func func() string

Func delays error string generation until the error string gets accessed. It does this by implementing the go Error() interface for parameterless functions. For example: funError:= errutil.Func(func() string { return "fun" })

func (Func) Error

func (e Func) Error() string

Error implements go's Error() interface for a function.

type NoPanic

type NoPanic interface{ NoPanic() }

NoPanic - custom errors can implement this to stop errutil from panicing. useful on occasion for provisional error types

type NoPanicError

type NoPanicError string

Error type which uses a single string as an error and which implements the generic NoPanic interface.

func (NoPanicError) Error

func (e NoPanicError) Error() string

func (NoPanicError) Is

func (e NoPanicError) Is(target error) bool

func (NoPanicError) NoPanic

func (e NoPanicError) NoPanic()

Jump to

Keyboard shortcuts

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