aerrors

package module
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: MIT Imports: 8 Imported by: 0

README

aerrors

Go Reference

Aerrors is package for Golang augmented errors.

Usage

Can be used like existing package.

Like an errors.New

err := aerrors.New("new error")

fmt.Println(err)
// Output:
// new error

Like a fmt.Errorf

err := aerrors.Errorf("error: %d", 42)

fmt.Println(err)
// Output:
// error: 42

Aerror's error has verbose information in addition to default errors.

err := aerrors.New("new error")

fmt.Printf("%+v", err)
// Output:
// new error:
//     priority: Error
//     callers: main.main:path/to/example/main.go:10

Can also add more information.

err := aerrors.New("new error").WithValue(
  aerrors.String("foo", "Foo"),
  aerrors.Int("number", 42)
)

fmt.Printf("%+v", err)
// Output:
// new error:
//     priority: Error
//     callers: main.main:path/to/example/main.go:10
//     foo: Foo
//     number: 42

Can also create extend error.

appError := aerrors.New("app error")
err := appError.New("oops")

fmt.Println(err)
fmt.Printf("is parent: %v", errors.Is(err, appError))
fmt.Printf("parent: %v", err.Parent)
// Output:
// oops
// is parent: true
// parent: app error

Can also create wrapped error.

appError := appError := aerrors.New("application error")
err := appError.Errorf("error: %w", errors.New("oops"))

fmt.Println(err)
// Output:
// error: oops:
//     priority: Error
//     parent: application error
//     callers: main.main:path/to/example/main.go:12
//   - oops
Trim GOPATH from callers and stack traces

Use -trimpath option. (see, Command go)

go build -trimpath

License

Aerrors is licensed under the MIT license.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultConfig = &Config{
	priority:    Error,
	formatError: NewFormatter("\n", ": "),
	callerDepth: 1,
	callerSkip:  0,
}

DefaultConfig for create *Err.

View Source
var DefaultStackDepth = 16

DefaultStackDepth is the depth used when call Stack.

View Source
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

func (c *Config) CallerDepth() int

CallerDepth returns specified caller depth.

func (*Config) CallerSkip

func (c *Config) CallerSkip() int

CallerSkip returns specified caller skip count.

func (*Config) Clone

func (c *Config) Clone() *Config

Clone *Config.

func (*Config) Error added in v1.3.0

func (c *Config) Error(msg string, opts ...Option) *Err

Error returns new aerror's error from Config.

func (*Config) Errorf added in v1.3.0

func (c *Config) Errorf(format string, args ...interface{}) *Err

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

func (c *Config) WithCallerDepth(n int) *Config

WithCallerDepth sets caller depth and return receiver.

func (*Config) WithCallerSkip added in v1.3.0

func (c *Config) WithCallerSkip(n int) *Config

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

func AsErr(err error) (e *Err, ok bool)

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

func Errorf(format string, args ...interface{}) *Err

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

func New(msg string, opts ...Option) *Err

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

func (e *Err) Callers() *runtime.Frames

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

func (e *Err) ChildConfig() *Config

ChildConfig returns *Config for new child.

func (*Err) Config deprecated

func (e *Err) Config() *Config

Config returns aerror's config.

Deprecated: Use *Err.ChildConfig.

Example
err := New("new error")

fmt.Println(err.ChildConfig().callerDepth)
Output:

1

func (*Err) Error

func (e *Err) Error() string

Error implements interface `error`.

func (*Err) Errorf

func (e *Err) Errorf(format string, args ...interface{}) *Err

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) Format

func (e *Err) Format(s fmt.State, verb rune)

Format implements interface `fmt.Formatter`

func (*Err) FormatError

func (e *Err) FormatError(p xerrors.Printer) (next error)

FormatError implements interface `xerrors.Formatter`

func (*Err) Is

func (e *Err) Is(err error) bool

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

func (e *Err) New(msg string, options ...Option) *Err

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) Parent

func (e *Err) Parent() *Err

Parent return parent *Err.

func (*Err) Priority

func (e *Err) Priority() ErrorPriority

Priority returns error priority.

func (*Err) Unwrap

func (e *Err) Unwrap() error

Unwrap error.

func (*Err) Values

func (e *Err) Values() []*Value

Values set by `WithValue` method.

func (*Err) WithBool added in v1.4.0

func (e *Err) WithBool(l string, v bool) *Err

WithBool appends bool Value and returns receiver.

func (*Err) WithByte added in v1.4.0

func (e *Err) WithByte(l string, v byte) *Err

WithByte appends byte Value and returns receiver.

func (*Err) WithBytes added in v1.4.0

func (e *Err) WithBytes(l string, v []byte) *Err

WithBytes appends bytes Value and returns receiver.

func (*Err) WithChildConfig added in v1.3.0

func (e *Err) WithChildConfig(c *Config) *Err

WithChildConfig sets *Config for new child and receiver.

func (*Err) WithError added in v1.3.0

func (e *Err) WithError(err error) *Err

WithError sets wrapped error and returns receiver.

func (*Err) WithFloat32 added in v1.4.0

func (e *Err) WithFloat32(l string, v float32) *Err

WithFloat32 appends Float32 Value and returns receiver.

func (*Err) WithFloat64 added in v1.4.0

func (e *Err) WithFloat64(l string, v float64) *Err

WithFloat64 appends Float64 Value and returns receiver.

func (*Err) WithInt added in v1.4.0

func (e *Err) WithInt(l string, v int) *Err

WithInt appends int Value and returns receiver.

func (*Err) WithInt16 added in v1.4.0

func (e *Err) WithInt16(l string, v int16) *Err

WithInt16 appends Int16 Value and returns receiver.

func (*Err) WithInt32 added in v1.4.0

func (e *Err) WithInt32(l string, v int32) *Err

WithInt32 appends Int32 Value and returns receiver.

func (*Err) WithInt64 added in v1.4.0

func (e *Err) WithInt64(l string, v int64) *Err

WithInt64 appends Int64 Value and returns receiver.

func (*Err) WithInt8 added in v1.4.0

func (e *Err) WithInt8(l string, v int8) *Err

WithInt8 appends Int8 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) WithRune added in v1.4.0

func (e *Err) WithRune(l string, v rune) *Err

WithRune appends rune Value and returns receiver.

func (*Err) WithStack added in v1.4.0

func (e *Err) WithStack(skip int) *Err

WithStack appends Stack Value and returns receiver.

func (*Err) WithStackN added in v1.4.0

func (e *Err) WithStackN(depth, skip int) *Err

WithStackN appends Stack Value and returns receiver.

func (*Err) WithString added in v1.4.0

func (e *Err) WithString(l, v string) *Err

WithString appends string Value and returns receiver.

func (*Err) WithStringer added in v1.4.0

func (e *Err) WithStringer(l string, v interface{ String() string }) *Err

WithStringer appends stringer Value and returns receiver.

func (*Err) WithStringf added in v1.4.0

func (e *Err) WithStringf(l string, format string, args ...interface{}) *Err

WithStringf appends formatted string Value and returns receiver.

func (*Err) WithTime added in v1.4.0

func (e *Err) WithTime(l string, v time.Time) *Err

WithTime appends Time Value and returns receiver.

func (*Err) WithUTCTime added in v1.4.0

func (e *Err) WithUTCTime(l string, v time.Time) *Err

WithUTCTime appends UTCTime Value and returns receiver.

func (*Err) WithUint added in v1.4.0

func (e *Err) WithUint(l string, v uint) *Err

WithUint appends Uint Value and returns receiver.

func (*Err) WithUint16 added in v1.4.0

func (e *Err) WithUint16(l string, v uint16) *Err

WithUint16 appends Uint16 Value and returns receiver.

func (*Err) WithUint32 added in v1.4.0

func (e *Err) WithUint32(l string, v uint32) *Err

WithUint32 appends Uint32 Value and returns receiver.

func (*Err) WithUint64 added in v1.4.0

func (e *Err) WithUint64(l string, v uint64) *Err

WithUint64 appends Uint64 Value and returns receiver.

func (*Err) WithUint8 added in v1.4.0

func (e *Err) WithUint8(l string, v uint8) *Err

WithUint8 appends Uint8 Value and returns receiver.

func (*Err) WithValue

func (e *Err) WithValue(values ...*Value) *Err

WithValue sets the `values` and returns receiver.

Example
err := New("new error").WithValue(String("str", "Foo"), Bool("bool", true))

for _, v := range err.Values() {
	fmt.Printf("%s: %s\n", v.Label, v.Value)
}
Output:

str: Foo
bool: true

func (*Err) Wrap

func (e *Err) Wrap(err error, opts ...Option) *Err

Wrap specified error `err`.

Example
appError := New("app error")
err := appError.Wrap(errors.New("oops"))

fmt.Println(err)
fmt.Println(err.Unwrap())
Output:

app error
oops

type ErrorFormatter

type ErrorFormatter func(xerrors.Printer, *Err) (next error)

ErrorFormatter is func for format error.

func NewFormatter

func NewFormatter(sep, labelSep string) ErrorFormatter

NewFormatter returns

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

type Option func(*Config) *Config

Option for create error `*Err`.

func CallerDepth

func CallerDepth(n int) Option

CallerDepth option configures callers depth.

func CallerSkip

func CallerSkip(n int) Option

CallerSkip option configures callers skip.

func Formatter

func Formatter(f ErrorFormatter) Option

Formatter option configures error formatter.

func Priority

func Priority(p ErrorPriority) Option

Priority option configures error priority.

type Value

type Value struct {
	Label string
	Value string
}

Value is labeled value.

Using `(*Err).With(values...)`

func Bool

func Bool(l string, v bool) *Value

Bool returns Value.

func Byte

func Byte(l string, b byte) *Value

Byte returns Value.

func Bytes

func Bytes(l string, v []byte) *Value

Bytes returns Value.

func Float32

func Float32(l string, v float32) *Value

Float32 returns Value of float32.

func Float64

func Float64(l string, v float64) *Value

Float64 returns Value of float64.

func Int

func Int(l string, v int) *Value

Int returns Value.

func Int16

func Int16(l string, v int16) *Value

Int16 returns Value.

func Int32

func Int32(l string, v int32) *Value

Int32 returns Value.

func Int64

func Int64(l string, v int64) *Value

Int64 returns Value.

func Int8

func Int8(l string, v int8) *Value

Int8 returns Value.

func Rune added in v1.3.0

func Rune(l string, v rune) *Value

Rune returns Value.

func Stack

func Stack(skip int) *Value

Stack returns Value of stack trace. depth is determined by DefaultStackDepth.

func StackN

func StackN(depth, skip int) *Value

StackN returns Value of stack trace for N layers.

func String

func String(l, v string) *Value

String returns Value.

func Stringer

func Stringer(l string, v interface{ String() string }) *Value

Stringer returns Value.

func Stringf added in v1.2.0

func Stringf(l string, format string, args ...interface{}) *Value

Stringf returns formatted string Value.

func Time

func Time(l string, v time.Time) *Value

Time returns Value of time.

func UTCTime

func UTCTime(l string, v time.Time) *Value

UTCTime returns Value of UTC time.

func Uint

func Uint(l string, v uint) *Value

Uint returns Value.

func Uint16

func Uint16(l string, v uint16) *Value

Uint16 returns Value.

func Uint32

func Uint32(l string, v uint32) *Value

Uint32 returns Value.

func Uint64

func Uint64(l string, v uint64) *Value

Uint64 returns Value.

func Uint8

func Uint8(l string, v uint8) *Value

Uint8 returns Value.

func (*Value) String

func (v *Value) String() string

String returns label and value string.

func (*Value) WithLabel

func (v *Value) WithLabel(l string) *Value

WithLabel sets the label `l` and returns it receiver.

type Values added in v1.3.0

type Values []*Value

func (Values) Bool added in v1.3.0

func (ls Values) Bool(l string, v bool) Values

Bool appends bool Value and return Values.

func (Values) Byte added in v1.3.0

func (ls Values) Byte(l string, v byte) Values

Byte appends byte Value and return Values.

func (Values) Bytes added in v1.3.0

func (ls Values) Bytes(l string, v []byte) Values

Bytes appends bytes Value and return Values.

func (Values) Float32 added in v1.3.0

func (ls Values) Float32(l string, v float32) Values

Float32 appends Float32 Value and return Values.

func (Values) Float64 added in v1.3.0

func (ls Values) Float64(l string, v float64) Values

Float64 appends Float64 Value and return Values.

func (Values) Int added in v1.3.0

func (ls Values) Int(l string, v int) Values

Int appends int Value and return Values.

func (Values) Int16 added in v1.3.0

func (ls Values) Int16(l string, v int16) Values

Int16 appends Int16 Value and return Values.

func (Values) Int32 added in v1.3.0

func (ls Values) Int32(l string, v int32) Values

Int32 appends Int32 Value and return Values.

func (Values) Int64 added in v1.3.0

func (ls Values) Int64(l string, v int64) Values

Int64 appends Int64 Value and return Values.

func (Values) Int8 added in v1.3.0

func (ls Values) Int8(l string, v int8) Values

Int8 appends Int8 Value and return Values.

func (Values) Rune added in v1.3.0

func (ls Values) Rune(l string, v rune) Values

Rune appends rune Value and return Values.

func (Values) Stack added in v1.3.0

func (ls Values) Stack(skip int) Values

Stack appends Stack Value and return Values.

func (Values) StackN added in v1.3.0

func (ls Values) StackN(depth, skip int) Values

StackN appends Stack Value and return Values.

func (Values) String added in v1.3.0

func (ls Values) String(l, v string) Values

String appends string Value and return Values.

func (Values) Stringer added in v1.3.0

func (ls Values) Stringer(l string, v interface{ String() string }) Values

Stringer appends stringer Value and return Values.

func (Values) Stringf added in v1.3.0

func (ls Values) Stringf(l string, format string, args ...interface{}) Values

Stringf appends formatted string Value and return Values.

func (Values) Time added in v1.3.0

func (ls Values) Time(l string, v time.Time) Values

Time appends Time Value and return Values.

func (Values) UTCTime added in v1.3.0

func (ls Values) UTCTime(l string, v time.Time) Values

UTCTime appends UTCTime Value and return Values.

func (Values) Uint added in v1.3.0

func (ls Values) Uint(l string, v uint) Values

Uint appends Uint Value and return Values.

func (Values) Uint16 added in v1.3.0

func (ls Values) Uint16(l string, v uint16) Values

Uint16 appends Uint16 Value and return Values.

func (Values) Uint32 added in v1.3.0

func (ls Values) Uint32(l string, v uint32) Values

Uint32 appends Uint32 Value and return Values.

func (Values) Uint64 added in v1.3.0

func (ls Values) Uint64(l string, v uint64) Values

Uint64 appends Uint64 Value and return Values.

func (Values) Uint8 added in v1.3.0

func (ls Values) Uint8(l string, v uint8) Values

Uint8 appends Uint8 Value and return Values.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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