Documentation
¶
Overview ¶
Package errors provides a lightweight error handling library with support for error wrapping, formatting, and error chain checking.
DO NOT use fmt.Errorf to create errors, use errors.New or errors.Errorf instead. ¶
fmt.Errorf is not compatible with errors.Is and errors.As.
fmt.Errorf("failed to process user %d: %w", userID, err)
is not the same as
errors.Errorf("failed to process user %d: %w", userID, err)
Example: ¶
// Create an error
err := errors.New("user validation failed").
With("user_id", 12345).
With("email", "user@example.com").
With("attempt", 3)
// Wrap the error
err = errors.Wrap(err, "using wrap to wrap the error")
err = errors.Errorf("%w can also wrap the error", err)
// Format the error
message := err.Error()
textWithStack := errors.Format(err)
jsonWithStack := errors.FormatJSON(err)
colorizedWithStack := errors.FormatColorized(err)
// using Sprintf to format the error
message := fmt.Sprintf("%s", err)
textWithStack := fmt.Sprintf("%v", err) // equal to errors.Format(err)
jsonWithStack := fmt.Sprintf("%#v", err) // equal to errors.FormatJSON(err)
colorizedWithStack := fmt.Sprintf("%+v", err) // equal to errors.FormatColorized(err)
// Check if the error is a specific error
if errors.Is(err, errors.New("user validation failed")) {
// handle the error
}
var validationErr AnErrorType
if errors.As(err, &validationErr) {
// handle the error
}
Example ¶
Example demonstrates how to use the errors package
err1 := New("something went wrong")
fmt.Println(err1.Error())
err2 := &errorStack{
message: "failed to process user: something went wrong",
cause: err1,
stack: getStack(0),
attr: []attr{},
}
fmt.Println(err2.Error())
err3 := New("database error").
With("table", "users").
With("operation", "insert")
fmt.Println(err3.(*errorStack).String())
fmt.Println("JSON:", FormatJson(err3))
fmt.Println("Colorized:", FormatColorized(err3))
Index ¶
- Variables
- func As(err error, target any) bool
- func Format(err error) string
- func FormatColorized(err error) string
- func FormatJson(err error) string
- func Is(err, target error) bool
- func Join(errs ...error) error
- func Unwrap(err error) error
- type Error
- type Template
- func (t Template) Attrs(lastCaller frame) []attr
- func (t Template) Clone() Template
- func (t Template) Errorf(format string, args ...any) Error
- func (t Template) New(text string) Error
- func (t Template) With(args ...any) Template
- func (t Template) WithMap(m map[string]any) Template
- func (t Template) Wrap(err error, args ...any) Error
- func (t Template) Wrapf(err error, format string, args ...any) Error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // SkipRuntimeStackTrace is a flag to skip the runtime stack trace // // It is useful to skip the runtime stack trace when you want to get the error message // without the runtime stack trace // // It is true by default SkipRuntimeStackTrace = true )
Functions ¶
func FormatColorized ¶
FormatColorized formats the error as a colorized string
func Join ¶ added in v1.0.2
Join returns an error that wraps the given errors. Any nil error values are discarded. Join returns nil if every value in errs is nil. The error formats as the concatenation of the strings obtained by calling the Error method of each element of errs, with a newline between each string.
A non-nil error returned by Join implements the Unwrap() []error method.
Types ¶
type Error ¶
Error is an interface that wraps the error interface and provides additional methods
It also implements the error interface, so it can be used as an error
type Template ¶ added in v0.0.6
type Template struct {
// contains filtered or unexported fields
}
Template is a template for creating errors. It contains args that can be used to create an error.
func NewTemplate ¶ added in v0.0.6
NewTemplate creates a new Template.
func (Template) Attrs ¶ added in v0.0.6
func (t Template) Attrs(lastCaller frame) []attr
Attrs returns a copy of the template's attributes with the Function field set to the provided lastCaller frame's Function value.
func (Template) Errorf ¶ added in v0.0.6
Errorf creates a new formatted Error using the template's attributes. It formats the message using fmt.Sprintf with the provided format and args.
func (Template) New ¶ added in v0.0.6
New creates a new Error with the given text message and the template's attributes.
func (Template) With ¶ added in v0.0.6
With creates a new Template by appending additional attributes to the existing ones. It returns a new Template instance without modifying the original one.
func (Template) WithMap ¶ added in v1.0.6
WithMap creates a new Template by appending additional attributes to the existing ones. It returns a new Template instance without modifying the original one.
