Documentation
¶
Overview ¶
Package errors provides error management.
By convention, wrapping functions return a nil error if the given error is nil.
Index ¶
- Variables
- func As(err error, target any) bool
- func Is(err, target error) bool
- func Message(err error, msg string) error
- func New(msg string) error
- func Stack(err error) error
- func StackFrames(err error) []*runtime.Frames
- func Unwrap(err error) error
- func Verbose(w io.Writer, err error)
- func VerboseFormatter(err error) fmt.Formatter
- func VerboseString(err error) string
- func Wrap(err error, msg string) error
- type Verboser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var StackFrameVerboseWriter = func(w io.Writer, f runtime.Frame) { _, file := filepath.Split(f.File) _, _ = fmt.Fprintf(w, "\t%s %s:%d\n", f.Function, file, f.Line) }
StackFrameVerboseWriter writes a runtime.Frame to an error verbose message.
It must write a new line character at the end.
It can be changed in order to customize how runtime.Frame are formatted.
Functions ¶
func Message ¶
Message adds a message to an error.
The error message is "<msg>: <err>".
If the given message is empty, the returned error is the given error.
// Use fmt.Sprintf() to format the message.
Example ¶
err := New("error")
err = Message(err, "message")
fmt.Println(err)
Output: message: error
func New ¶
New returns a new error with a message and a stack.
Use fmt.Sprintf() to format the message.
Example ¶
err := New("error")
fmt.Println(err)
Output: error
func Stack ¶
Stack adds a stack to an error.
The verbose message contains the stack.
See https://pkg.go.dev/runtime#Frames .
Example ¶
err := New("error")
err = Stack(err)
fmt.Println(err)
sfs := StackFrames(err)
fmt.Println(len(sfs))
Output: error 2
func StackFrames ¶
StackFrames returns the list of runtime.Frames associated to an error.
func Verbose ¶
Verbose writes the error's verbose message to the writer.
The first line is the error's message. The following lines are the verbose message of the error chain.
Example ¶
err := New("error")
buf := new(strings.Builder)
Verbose(buf, err)
s := buf.String()
fmt.Println(s)
func VerboseFormatter ¶ added in v0.0.2
VerboseFormatter returns a fmt.Formatter that writes the error's verbose message.
Example ¶
err := New("error")
f := VerboseFormatter(err)
fmt.Println(f)
func VerboseString ¶
VerboseString returns the error's verbose message as a string.
Example ¶
err := New("error")
s := VerboseString(err)
fmt.Println(s)
Types ¶
type Verboser ¶
type Verboser interface {
// ErrorVerbose writes the verbose message of the error to the writer.
// It must only write the verbose message of the error, not the error chain.
// It is responsible for writing a new line character at the end.
ErrorVerbose(io.Writer)
}
Verboser is an error that provides verbose information.
It is used by Verbose().
Directories
¶
| Path | Synopsis |
|---|---|
|
Package errignore provides a way to mark errors as ignored.
|
Package errignore provides a way to mark errors as ignored. |
|
Package errtag provides a way to add tags to errors.
|
Package errtag provides a way to add tags to errors. |
|
Package errtmp provides a way to mark errors as temporary.
|
Package errtmp provides a way to mark errors as temporary. |
|
Package errval provides a way to add values to errors.
|
Package errval provides a way to add values to errors. |