errors

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2018 License: Apache-2.0 Imports: 7 Imported by: 9

README

errors

Travis-CI Go Report Card GoDoc codecov

An errors package built on top of existing battle tested packages with structured JSON serialization support.

Requirements

Go v1.10+ since strings.Builder API is used for generating the output for Error() method.

Features

  • A very primitive support for monads
  • Maintains backwards compatibility with Dave Cheney's errors package.
  • Additional functions for wrapping errors inspired by Upspin's error package.
  • Additional methods for error inspection.
  • JSON serialization support.

Prior Art

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func Errorf

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

Errorf is equivalent to fmt.Errorf, but allows clients to import only this package for all error handling.

func IsKind added in v0.2.3

func IsKind(err error, k Kind) bool

IsKind returns a boolean signifying if the error matches the Kind provided in the input

func New

func New(msg string, args ...interface{}) error

New builds an error value from its arguments. There must be at least one argument or New panics. The type of each argument determines its meaning. If more than one argument of a given type is presented, only the last one is recorded.

The types are:

errors.op
	The operation being performed, usually the method
	being invoked (Get, Put, etc.).
errors.kind
	The class of error, such as permission failure.
error
	The underlying error that triggered this one.

If the error is printed, only those items that have been set to non-zero values will appear in the result.

If Kind is not specified or Internal, we set it to the Kind of the underlying error.

func Str

func Str(text string) error

Str returns an error that formats as the given text. It is intended to be used as the error-typed argument to the E function.

func WithKind

func WithKind(err error, kind Kind, msg string) error

WithKind returns an error annotating err with the service specific kind of err at the point WithKind is called. If err is nil, WithKind returns nil.

func WithKindf

func WithKindf(err error, kind Kind, format string, args ...interface{}) error

WithKindf returns an error annotating err with the service specific kind of err at the point WithKindf is call, and the format specifier. If err is nil, WithKindf returns nil.

func WithMessage

func WithMessage(err error, message string) error

WithMessage annotates err with a new message. If err is nil, WithMessage returns nil.

func WithMessagef

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

WithMessagef formats an err with a new message. If err is nil, WithMessage returns nil.

func WithOp

func WithOp(err error, op Op) error

WithOp returns an error annotating err with a hint of the operation name at the point WithOp is called. If err is nil, WithOp returns nil.

func WithOpf

func WithOpf(err error, op Op, format string, args ...interface{}) error

WithOpf returns an error annotating err with a hint of the operation name at the point WithOpf is call, and the format specifier. If err is nil, WithOpf returns nil.

func WithStack

func WithStack(err error) error

WithStack annotates err with a stack trace at the point WithStack was called. If err is nil, WithStack returns nil.

func Wrap

func Wrap(err error, message string) error

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.

func Wrapf

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

Wrapf returns an error annotating err with a stack trace at the point Wrapf is call, and the format specifier. If err is nil, Wrapf returns nil.

Types

type Error

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

Error is the type that implements the error interface. It contains a number of fields, each of different type. An Error value may leave some values unset.

func Serializable added in v0.2.3

func Serializable(err error) *Error

Serializable returns a serializable error struct that cna omit exposing internal errors as an option in release mode

func Unwrap

func Unwrap(err error) *Error

Unwrap returns the first occurrence of the underlying *Error type using causer or create a new *Error type containing the original error's message

func (*Error) Cause

func (e *Error) Cause() error

Cause specifies the Cause of the underlying error wrapped inside

func (*Error) Error

func (e *Error) Error() string

func (*Error) Kind

func (e *Error) Kind() Kind

Kind specifies the Kind of the error

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON converts the err object to the JSON representation

func (*Error) UnmarshalJSON

func (e *Error) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes JSON back to Error struct

type Kind

type Kind int

Kind defines the kind of error this is, mostly for use by systems such as FUSE that must act differently depending on the error.

const (
	Internal       Kind = iota // Internal error or inconsistency.
	Invalid                    // Invalid operation for this type of item.
	Permission                 // Permission denied.
	IO                         // External I/O error such as network failure.
	AlreadyExist               // Item already exists.
	NotExist                   // Item does not exist.
	NotImplemented             // Method not implemented
)

Kinds of errors.

The values of the error kinds are common between both clients and servers. Do not reorder this list or remove any items since that will change their values. New items must be added only to the end.

func KindOf

func KindOf(err error) Kind

KindOf returns the underlying kind type of the error, if possible. An error value has a kind if it implements the following interface:

type kinder interface {
       Kind() Kind
}

If the error does not implement Kind, the original error will be returned. If the error is nil, Internal will be returned without further investigation.

func (Kind) String

func (k Kind) String() string

type Monad

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

Monad is a container for error type which could be either nil or not

func Do

func Do(fn failableFunc) Monad

Do returns a monad

func (Monad) Defer

func (e Monad) Defer(fn func()) Monad

Defer adds the defer func

func (Monad) Do

func (e Monad) Do(fn failableFunc) Monad

Do returns a monad

func (Monad) Err

func (e Monad) Err() error

Err returns an error or nil

type Op

type Op string

Op describes an operation, usually as the package and method, such as "key/server.Lookup".

Jump to

Keyboard shortcuts

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