errors

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2018 License: MIT Imports: 4 Imported by: 50

README

Errors

A error package supports wrapping and multi error

Issues

Implementation

  • Wrap checks if this is already a WrappedErr, if not, it attach stack
  • MultiErr keep a slice of errors, the thread safe version use mutex and returns copy of slice when Errors is called

Survey

Documentation

Overview

Package errors provides multi error, error wrapping. It defines error category code for machine post processing

Index

Examples

Constants

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

func Cause(err error) error

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

func DirectCause(err error) error

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 Errorf

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

Errorf is New with fmt.Sprintf

func IsRuntimeError

func IsRuntimeError(err error) bool

func New

func New(msg string) error

New creates a FreshError with stack

func PrintFrames added in v0.0.8

func PrintFrames(frames []runtime.Frame)

func Wrap

func Wrap(err error, msg string) error

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

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf is Wrap with fmt.Sprintf

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

func (*FreshError) Format

func (fresh *FreshError) Format(s fmt.State, verb rune)

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 NewMultiErr

func NewMultiErr() MultiErr

NewMultiErr returns a non thread safe implementation

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 Stack

type Stack struct {
	// contains filtered or unexported fields
}

func (*Stack) Frames

func (s *Stack) Frames() []runtime.Frame

type TracedError

type TracedError interface {
	fmt.Formatter
	ErrorStack() *Stack
}

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

func (wrapped *WrappedError) Format(s fmt.State, verb rune)

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

Jump to

Keyboard shortcuts

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