Documentation ¶
Index ¶
- Variables
- type Config
- func (c *Config) CallerDepth() int
- func (c *Config) CallerSkip() int
- func (c *Config) Clone() *Config
- func (c *Config) Error(msg string, opts ...Option) *Err
- func (c *Config) Errorf(format string, args ...interface{}) *Err
- func (c *Config) Formatter() ErrorFormatter
- func (c *Config) Priority() ErrorPriority
- func (c *Config) WithCallerDepth(n int) *Config
- func (c *Config) WithCallerSkip(n int) *Config
- func (c *Config) WithFormatter(f ErrorFormatter) *Config
- func (c *Config) WithPriority(p ErrorPriority) *Config
- type Err
- func (e *Err) Callers() *runtime.Frames
- func (e *Err) ChildConfig() *Config
- func (e *Err) Config() *Configdeprecated
- func (e *Err) Error() string
- func (e *Err) Errorf(format string, args ...interface{}) *Err
- func (e *Err) Format(s fmt.State, verb rune)
- func (e *Err) FormatError(p xerrors.Printer) (next error)
- func (e *Err) Is(err error) bool
- func (e *Err) New(msg string, options ...Option) *Err
- func (e *Err) Parent() *Err
- func (e *Err) Priority() ErrorPriority
- func (e *Err) Unwrap() error
- func (e *Err) Values() []*Value
- func (e *Err) WithBool(l string, v bool) *Err
- func (e *Err) WithByte(l string, v byte) *Err
- func (e *Err) WithBytes(l string, v []byte) *Err
- func (e *Err) WithChildConfig(c *Config) *Err
- func (e *Err) WithError(err error) *Err
- func (e *Err) WithFloat32(l string, v float32) *Err
- func (e *Err) WithFloat64(l string, v float64) *Err
- func (e *Err) WithInt(l string, v int) *Err
- func (e *Err) WithInt16(l string, v int16) *Err
- func (e *Err) WithInt32(l string, v int32) *Err
- func (e *Err) WithInt64(l string, v int64) *Err
- func (e *Err) WithInt8(l string, v int8) *Err
- func (e *Err) WithPriority(p ErrorPriority) *Err
- func (e *Err) WithRune(l string, v rune) *Err
- func (e *Err) WithStack(skip int) *Err
- func (e *Err) WithStackN(depth, skip int) *Err
- func (e *Err) WithString(l, v string) *Err
- func (e *Err) WithStringer(l string, v interface{ ... }) *Err
- func (e *Err) WithStringf(l string, format string, args ...interface{}) *Err
- func (e *Err) WithTime(l string, v time.Time) *Err
- func (e *Err) WithUTCTime(l string, v time.Time) *Err
- func (e *Err) WithUint(l string, v uint) *Err
- func (e *Err) WithUint16(l string, v uint16) *Err
- func (e *Err) WithUint32(l string, v uint32) *Err
- func (e *Err) WithUint64(l string, v uint64) *Err
- func (e *Err) WithUint8(l string, v uint8) *Err
- func (e *Err) WithValue(values ...*Value) *Err
- func (e *Err) Wrap(err error, opts ...Option) *Err
- type ErrorFormatter
- type ErrorPriority
- type Option
- type Value
- func Bool(l string, v bool) *Value
- func Byte(l string, b byte) *Value
- func Bytes(l string, v []byte) *Value
- func Float32(l string, v float32) *Value
- func Float64(l string, v float64) *Value
- func Int(l string, v int) *Value
- func Int16(l string, v int16) *Value
- func Int32(l string, v int32) *Value
- func Int64(l string, v int64) *Value
- func Int8(l string, v int8) *Value
- func Rune(l string, v rune) *Value
- func Stack(skip int) *Value
- func StackN(depth, skip int) *Value
- func String(l, v string) *Value
- func Stringer(l string, v interface{ ... }) *Value
- func Stringf(l string, format string, args ...interface{}) *Value
- func Time(l string, v time.Time) *Value
- func UTCTime(l string, v time.Time) *Value
- func Uint(l string, v uint) *Value
- func Uint16(l string, v uint16) *Value
- func Uint32(l string, v uint32) *Value
- func Uint64(l string, v uint64) *Value
- func Uint8(l string, v uint8) *Value
- type Values
- func (ls Values) Bool(l string, v bool) Values
- func (ls Values) Byte(l string, v byte) Values
- func (ls Values) Bytes(l string, v []byte) Values
- func (ls Values) Float32(l string, v float32) Values
- func (ls Values) Float64(l string, v float64) Values
- func (ls Values) Int(l string, v int) Values
- func (ls Values) Int16(l string, v int16) Values
- func (ls Values) Int32(l string, v int32) Values
- func (ls Values) Int64(l string, v int64) Values
- func (ls Values) Int8(l string, v int8) Values
- func (ls Values) Rune(l string, v rune) Values
- func (ls Values) Stack(skip int) Values
- func (ls Values) StackN(depth, skip int) Values
- func (ls Values) String(l, v string) Values
- func (ls Values) Stringer(l string, v interface{ ... }) Values
- func (ls Values) Stringf(l string, format string, args ...interface{}) Values
- func (ls Values) Time(l string, v time.Time) Values
- func (ls Values) UTCTime(l string, v time.Time) Values
- func (ls Values) Uint(l string, v uint) Values
- func (ls Values) Uint16(l string, v uint16) Values
- func (ls Values) Uint32(l string, v uint32) Values
- func (ls Values) Uint64(l string, v uint64) Values
- func (ls Values) Uint8(l string, v uint8) Values
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = &Config{ priority: Error, formatError: NewFormatter("\n", ": "), callerDepth: 1, callerSkip: 0, }
DefaultConfig for create *Err.
var DefaultStackDepth = 16
DefaultStackDepth is the depth used when call Stack.
var PriorityNames = map[ErrorPriority]string{ Emergency: "Emergency", Alert: "Alert", Critical: "Critical", Error: "Error", Warning: "Warning", Notice: "Notice", Info: "Info", Debug: "Debug", }
PriorityNames for (ErrorPriority).Name
It can overwrite for user defined priority.
aerrors.PriorityNames = map[aerrors.ErrorPriority]string{ Foo: "foo error", }
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config for create *Err
func (*Config) CallerDepth ¶
CallerDepth returns specified caller depth.
func (*Config) CallerSkip ¶
CallerSkip returns specified caller skip count.
func (*Config) Errorf ¶ added in v1.3.0
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
If the format specifier has suffix `: %w` verb with an error operand, the returned error will implement an Unwrap method returning the operand.
func (*Config) Formatter ¶ added in v1.3.0
func (c *Config) Formatter() ErrorFormatter
Formatter returns formater that format error messages. It is called by (*Err).FormatError.
func (*Config) Priority ¶
func (c *Config) Priority() ErrorPriority
Priority represents error priority.
func (*Config) WithCallerDepth ¶ added in v1.3.0
WithCallerDepth sets caller depth and return receiver.
func (*Config) WithCallerSkip ¶ added in v1.3.0
WithCallerSkip sets caller skip and return receiver.
func (*Config) WithFormatter ¶ added in v1.3.0
func (c *Config) WithFormatter(f ErrorFormatter) *Config
WithFormatter sets ErrorFormatter and return receiver.
func (*Config) WithPriority ¶ added in v1.3.0
func (c *Config) WithPriority(p ErrorPriority) *Config
WithPriority sets ErrorPriority and return receiver.
type Err ¶
type Err struct {
// contains filtered or unexported fields
}
Err is aerror's error. It implements interface `error`.
func AsErr ¶ added in v1.3.0
AsErr returns casted `*Err` error and whether cas succeeded.
It is the same as the code below.
var e *aerrors.Err ok := errors.As(&e)
Example ¶
parent := New("parent error") child := parent.New("child error") other := errors.New("other error") wrapped := fmt.Errorf("wrapped error: %w", child) cases := []error{ parent, child, other, wrapped, } for i, err := range cases { e, ok := AsErr(err) fmt.Printf("#%d: %v, %v\n", i, ok, e) }
Output: #0: true, parent error #1: true, child error #2: false, <nil> #3: true, child error
func Errorf ¶
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
If the format specifier has suffix `: %w` verb with an error operand, the returned error will implement an Unwrap method returning the operand.
Example ¶
err := Errorf("error: %d", 42) fmt.Println(err)
Output: error: 42
Example (With_wrapped_error) ¶
origin := errors.New("oops") err := Errorf("wrap error: %w", origin) fmt.Println(err) fmt.Println(err.Unwrap())
Output: wrap error: oops oops
func New ¶
New aerror's error with options.
Example ¶
err := New("new error") fmt.Println(err.Error())
Output: new error
Example (ChildVerbose) ¶
err := Errorf("new error: %w", New("oops")) fmt.Printf("%+v", err)
Output: new error: oops: priority: Error callers: aerrors.ExampleNew_childVerbose:github.com/kamiaka/aerrors/error_test.go:27 - oops: priority: Error callers: aerrors.ExampleNew_childVerbose:github.com/kamiaka/aerrors/error_test.go:27
Example (Verbose) ¶
err := New("new error") fmt.Printf("%+v", err)
Output: new error: priority: Error callers: aerrors.ExampleNew_verbose:github.com/kamiaka/aerrors/error_test.go:17
Example (With_options) ¶
err := New("new error", Priority(Emergency)) fmt.Println(err.Priority())
Output: Emergency
func (*Err) Callers ¶
Callers returns error callers.
Example ¶
err := New("new error") f := err.Callers() for { frame, more := f.Next() fmt.Println(frame.Function) if !more { break } }
Output: github.com/kamiaka/aerrors.ExampleErr_Callers
func (*Err) ChildConfig ¶ added in v1.3.0
ChildConfig returns *Config for new child.
func (*Err) Errorf ¶
Errorf returns new child *Err with message.
Example ¶
appError := New("app error") err := appError.Errorf("error: %d", 42) fmt.Println(err)
Output: error: 42
func (*Err) FormatError ¶
FormatError implements interface `xerrors.Formatter`
func (*Err) Is ¶
Is reports whether the error `err` is `e`
Example ¶
parent := New("parent error") err := parent.New("child error") other := errors.New("other error") otherChild := parent.New("other child error") fmt.Printf("err: %v\n", errors.Is(err, err)) fmt.Printf("parent: %v\n", errors.Is(err, parent)) fmt.Printf("friped: %v\n", errors.Is(parent, err)) fmt.Printf("other: %v\n", errors.Is(err, other)) fmt.Printf("other child: %v\n", errors.Is(err, otherChild))
Output: err: true parent: true friped: false other: false other child: false
func (*Err) New ¶
New child *Err.
Example ¶
appError := New("app error") err := appError.New("oops") fmt.Println(err) fmt.Println(err.Parent())
Output: oops app error
Example (WithOption) ¶
appError := New("app error") err := appError.New("oops", Priority(Info)) fmt.Println(err.Priority()) fmt.Println(err.Parent().Priority())
Output: Info Error
func (*Err) WithChildConfig ¶ added in v1.3.0
WithChildConfig sets *Config for new child and receiver.
func (*Err) WithFloat32 ¶ added in v1.4.0
WithFloat32 appends Float32 Value and returns receiver.
func (*Err) WithFloat64 ¶ added in v1.4.0
WithFloat64 appends Float64 Value and returns receiver.
func (*Err) WithPriority ¶
func (e *Err) WithPriority(p ErrorPriority) *Err
WithPriority sets error priority and returns receiver.
Example ¶
err := New("new error") fmt.Println(err.Priority()) err.WithPriority(Info) fmt.Println(err.Priority())
Output: Error Info
func (*Err) WithStackN ¶ added in v1.4.0
WithStackN appends Stack Value and returns receiver.
func (*Err) WithString ¶ added in v1.4.0
WithString appends string Value and returns receiver.
func (*Err) WithStringer ¶ added in v1.4.0
WithStringer appends stringer Value and returns receiver.
func (*Err) WithStringf ¶ added in v1.4.0
WithStringf appends formatted string Value and returns receiver.
func (*Err) WithUTCTime ¶ added in v1.4.0
WithUTCTime appends UTCTime Value and returns receiver.
func (*Err) WithUint16 ¶ added in v1.4.0
WithUint16 appends Uint16 Value and returns receiver.
func (*Err) WithUint32 ¶ added in v1.4.0
WithUint32 appends Uint32 Value and returns receiver.
func (*Err) WithUint64 ¶ added in v1.4.0
WithUint64 appends Uint64 Value and returns receiver.
type ErrorFormatter ¶
ErrorFormatter is func for format error.
type ErrorPriority ¶
type ErrorPriority int
ErrorPriority is error priority. Smaller value is higher priority.
const ( Emergency ErrorPriority = iota Alert Critical Error Warning Notice Info Debug )
Built in priorities.
func (ErrorPriority) HigherThan ¶
func (p ErrorPriority) HigherThan(q ErrorPriority) bool
HigherThan reports whether the priority s is higher priority than t.
func (ErrorPriority) String ¶
func (p ErrorPriority) String() string
type Option ¶
Option for create error `*Err`.
func Formatter ¶
func Formatter(f ErrorFormatter) Option
Formatter option configures error formatter.
type Value ¶
Value is labeled value.
Using `(*Err).With(values...)`
type Values ¶ added in v1.3.0
type Values []*Value