errors

package
v0.0.0-...-ea6506d Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package errors contains the error handling used by eventdb. It is largely inspired by (and contains code from) the upspin.io/errors package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func E

func E(args ...interface{}) error

E builds an error value from its arguments. There must be at least one argument or E 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.

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 Other, we set it to the Kind of the underlying error.

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 Is

func Is(kind Kind, err error) bool

Is reports whether err is an *Error of the given Kind. If err is nil then Is returns false.

func Match

func Match(err1, err2 error) bool

Match compares its two error arguments. It can be used to check for expected errors in tests. Both arguments must have underlying type *Error or Match will return false. Otherwise it returns true iff every non-zero element of the first error is equal to the corresponding element of the second. If the Err field is a *Error, Match recurs on that field; otherwise it compares the strings returned by the Error methods. Elements that are in the second argument but not present in the first are ignored.

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.

Types

type Error

type Error struct {
	// UserID is the name of the user attempting the operation.
	UserID eventdb.UserID
	// Op is the operation being performed, usually the name of the
	// method being invoked.
	Op Op
	// Kind is the class of error, such as permission failure, or "Other"
	// if its class is unknown or irrelevant.
	Kind Kind
	// The underlying error that triggered this one, if any.
	Err error
	// contains filtered or unexported fields
}

Error is a domain error for eventdb. It contains fields used to populate parts of the error message. Some of the fields may be left unset.

func (*Error) Error

func (e *Error) Error() string

type Kind

type Kind int

Kind defines the kind of error this is, used for translating into HTTP status codes based on the type of error.

const (
	Other       Kind = iota // Unclassified error. This value is not printed in the error message.
	Invalid                 // Bad request
	NotLoggedIn             // Unauthorized.
	Permission              // Permission denied.
	NotExist                // Item does not exist.
	Exist                   // Item already exists.
	Internal                // Internal error or inconsistency.
)

func (Kind) String

func (k Kind) String() string

type Op

type Op string

Op describes an operation. eg, "Service.EventGet"

type Response

type Response struct {
	Error   string      `json:"error,omitempty"`
	Details interface{} `json:"details,omitempty"`
	Status  int         `json:"status,omitempty"`
}

Response is a JSON-serializable version of an Error. It can be used to transmit errors across the REST API.

func ResponseForError

func ResponseForError(err error) Response

ResponseForError constructs an ErrorResponse based on an Error. Since this object is user-visible it's not a 1-1 mapping. Some errors will return detailed information about why the error happened in the Error and Details sections. Others wil just return an opaque error type.

func (Response) ToError

func (e Response) ToError() error

ToError converts an ErrorResponse back into an Error

Jump to

Keyboard shortcuts

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