Documentation
¶
Overview ¶
Package errors provides multi error, error wrapping. It defines error category code for machine post processing
Index ¶
- Constants
- func Cause(err error) error
- func DirectCause(err error) error
- func Errorf(format string, args ...interface{}) error
- func IsRuntimeError(err error) bool
- func New(msg string) error
- func PrintFrames(frames []runtime.Frame)
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type Causer
- type FreshError
- type MultiErr
- type Stack
- type TracedError
- type WrappedError
- type Wrapper
Examples ¶
Constants ¶
const ( // MultiErrSep is the separator used when returning a slice of errors as single line message MultiErrSep = "; " // ErrCauseSep is the separator used when returning a error with causal chain as single line message ErrCauseSep = ": " )
Variables ¶
This section is empty.
Functions ¶
func Cause ¶ added in v0.0.8
Cause returns root cause of the error (if any), it stops at the last error that does not implement Causer interface. If you want get direct cause, use DirectCause. If error is nil, it will return nil. If error is not wrapped it will return the error itself. error wrapped using https://github.com/pkg/errors also satisfies this interface and can be unwrapped as well.
func DirectCause ¶ added in v0.0.8
DirectCause returns the direct cause of the error (if any). It does NOT follow the cause chain, if you want to get root cause, use Cause
func IsRuntimeError ¶
func PrintFrames ¶ added in v0.0.8
func Wrap ¶
Wrap creates a WrappedError with stack and set its cause to err.
If the error being wrapped is already a TracedError, Wrap will reuse its stack trace instead of creating a new one. The error being wrapped has deeper stack than where the Wrap function is called and is closer to the root of error. This is based on https://github.com/pkg/errors/pull/122 to avoid having extra interface like WithMessage and WithStack like https://github.com/pkg/errors does.
Wrap returns nil if the error you are trying to wrap is nil, thus if it is the last error checking, you can return the wrap function directly in one line instead of using typical three line error check and wrap. i.e.
return errors.Wrap(f.Close(), "failed to close file")
if err := f.Close(); err != nil {
return errors.Wrap(err, "failed to close file")
}
return nil
Example ¶
package main
import (
"fmt"
"os"
"github.com/dyweb/gommon/errors"
)
func main() {
err := errors.Wrap(os.ErrNotExist, "oops")
fmt.Println(err)
}
Output: oops: file does not exist
Types ¶
type Causer ¶ added in v0.0.8
type Causer interface {
Cause() error
}
Causer returns the underlying error, a error without cause should return itself. It is based on the private `causer` interface in pkg/errors, so errors wrapped using pkg/errors can also be handled
type FreshError ¶
type FreshError struct {
// contains filtered or unexported fields
}
func (*FreshError) Error ¶
func (fresh *FreshError) Error() string
func (*FreshError) ErrorStack ¶
func (fresh *FreshError) ErrorStack() *Stack
type MultiErr ¶
type MultiErr interface {
error
fmt.Formatter
// Append adds error to current selection, it will flatten the error being added if it is also MultiErr
// It returns true if the appended error is not nil, inspired by https://github.com/uber-go/multierr/issues/21
Append(error) bool
// Errors returns errors stored, if no error
Errors() []error
// ErrorOrNil returns itself or nil if there are no errors, inspired by https://github.com/hashicorp/go-multierror
ErrorOrNil() error
// HasError is ErrorOrNil != nil
HasError() bool
}
MultiErr is a slice of Error. It has two implementation, NewMultiErr return a non thread safe version, NewMultiErrSafe return a thread safe version using mutex
Example ¶
package main
import (
"fmt"
"os"
"github.com/dyweb/gommon/errors"
)
func main() {
// TODO: demo the return value of append
err := errors.NewMultiErr()
err.Append(os.ErrPermission)
err.Append(os.ErrNotExist)
fmt.Println(err.Error())
fmt.Println(err.Errors())
}
Output: 2 errors; permission denied; file does not exist [permission denied file does not exist]
func NewMultiErrSafe ¶
func NewMultiErrSafe() MultiErr
NewMultiErrSafe returns a thread safe implementation which protects the underlying slice using mutex. It returns a copy of slice when Errors is called
type TracedError ¶
TracedError is error with stack trace
type WrappedError ¶
type WrappedError struct {
// contains filtered or unexported fields
}
func (*WrappedError) Cause ¶ added in v0.0.8
func (wrapped *WrappedError) Cause() error
func (*WrappedError) Error ¶
func (wrapped *WrappedError) Error() string
func (*WrappedError) ErrorStack ¶
func (wrapped *WrappedError) ErrorStack() *Stack
func (*WrappedError) Message ¶ added in v0.0.8
func (wrapped *WrappedError) Message() string
type Wrapper ¶ added in v0.0.8
type Wrapper interface {
Causer
// Message return the top level error message without concat message from its cause
// i.e. when Error() returns `invalid config: file a.json does not exist` Message() returns `invalid config`
Message() string
}
Wrapper has cause and its own error message. It is based on the private `wrapper` interface in juju/errors