Documentation ¶
Index ¶
- func Tags(err error) []string
- func Values(err error) map[string]any
- type Builder
- type Error
- func (x *Error) Error() string
- func (x *Error) Format(s fmt.State, verb rune)
- func (x *Error) HasTag(tag Tag) bool
- func (x *Error) ID(id string) *Error
- func (x *Error) Is(target error) bool
- func (x *Error) LogValue() slog.Value
- func (x *Error) Printable() *Printable
- func (x *Error) StackTrace() StackTrace
- func (x *Error) Stacks() []*Stack
- func (x *Error) Tags() []string
- func (x *Error) Unstack() *Error
- func (x *Error) UnstackN(n int) *Error
- func (x *Error) Unwrap() error
- func (x *Error) Values() map[string]any
- func (x *Error) With(key string, value any) *Error
- func (x *Error) WithTags(tags ...Tag) *Error
- func (x *Error) Wrap(cause error) *Error
- type Printable
- type Stack
- type StackTrace
- type Tag
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Builder ¶ added in v0.2.0
type Builder struct {
// contains filtered or unexported fields
}
Builder keeps a set of key-value pairs and can create a new error and wrap error with the key-value pairs.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is error interface for deepalert to handle related variables
func Unwrap ¶ added in v0.1.6
Unwrap returns unwrapped goerr.Error from err by errors.As. If no goerr.Error, returns nil NOTE: Do not receive error interface. It causes typed-nil problem.
var err error = goerr.New("error") if err != nil { // always true
func Wrapf ¶ added in v0.1.12
Wrapf creates a new Error and add message. The error message is formatted by fmt.Sprintf.
func (*Error) Format ¶
Format returns: - %v, %s, %q: formatted message - %+v: formatted message with stack trace
func (*Error) Is ¶ added in v0.1.1
Is returns true if target is goerr.Error and Error.id of two errors are matched. It's for errors.Is. If Error.id is empty, it always returns false.
func (*Error) LogValue ¶ added in v0.1.9
LogValue returns slog.Value for structured logging. It's implementation of slog.LogValuer. https://pkg.go.dev/log/slog#LogValuer
func (*Error) StackTrace ¶
func (x *Error) StackTrace() StackTrace
StackTrace returns stack trace that is compatible with pkg/errors
func (*Error) Tags ¶ added in v0.3.0
Tags returns list of tags that is set by WithTags. All wrapped goerr.Error tags will be merged. Tags of wrapped error is overwritten by upper goerr.Error.
func (*Error) Unstack ¶ added in v0.1.14
Unstack trims stack trace by 1. It can be used for internal helper or utility functions.
func (*Error) UnstackN ¶ added in v0.1.14
UnstackN trims stack trace by n. It can be used for internal helper or utility functions.
func (*Error) Values ¶
Values returns map of key and value that is set by With. All wrapped goerr.Error key and values will be merged. Key and values of wrapped error is overwritten by upper goerr.Error.
type StackTrace ¶
type StackTrace []frame
StackTrace is array of frame. It's exported for compatibility with github.com/pkg/errors
func (StackTrace) Format ¶
func (st StackTrace) Format(s fmt.State, verb rune)
Format formats the stack of Frames according to the fmt.Formatter interface.
%s lists source files for each Frame in the stack %v lists the source file and line number for each Frame in the stack
Format accepts flags that alter the printing of some verbs, as follows:
%+v Prints filename, function, and line number for each Frame in the stack.
type Tag ¶ added in v0.3.0
type Tag struct {
// contains filtered or unexported fields
}
Tag is a type to represent an error tag. It is used to categorize errors. The struct should be created by only NewTag function.
Example:
TagNotFound := NewTag("not_found") func FindUser(id string) (*User, error) { ... if user == nil { return nil, goerr.New("user not found").WithTags(TagNotFound) } ... } func main() { err := FindUser("123") if goErr := goerr.Unwrap(err); goErr != nil { if goErr.HasTag(TagNotFound) { fmt.Println("User not found") } } }
func NewTag ¶ added in v0.3.0
NewTag creates a new Tag. The key will be empty.
Example ¶
package main import ( "fmt" "github.com/m-mizutani/goerr" ) func main() { t1 := goerr.NewTag("DB error") err := goerr.New("error message").WithTags(t1) if goErr := goerr.Unwrap(err); goErr != nil { if goErr.HasTag(t1) { fmt.Println("DB error") } } }
Output: DB error