Documentation ¶
Overview ¶
Package errors defines an enhanced error handling. Based on upspin.io/errors.
Index ¶
- Variables
- func E(args ...interface{}) error
- func Errorf(format string, args ...interface{}) error
- func Is(kind Kind, err error) bool
- func MarshalError(err error) []byte
- func MarshalErrorAppend(err error, b []byte) []byte
- func Match(err1, err2 error) bool
- func RootCause(err error) error
- func Str(text string) error
- func Trap(values ...interface{}) error
- func UnmarshalError(b []byte) error
- func Wrapf(err error, msg string, args ...interface{}) error
- type Error
- type Kind
- type Op
Constants ¶
This section is empty.
Variables ¶
var Separator = ":\n\t"
Separator is the string used to separate nested errors. By default, to make errors easier on the eye, nested errors are indented on a new line. A server may instead choose to keep each error on a single line by modifying the separator string, perhaps to ":: ".
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.
The types are:
errors.Op The operation being performed, usually the method being invoked (Get, Put, etc.). string Treated as an error message and assigned to the Err field after a call to errors.Str. 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 Other, we set it to the Kind of the underlying error.
func Errorf ¶
Errorf is equivalent to fmt.Errorf, but allows clients to import only this package for all error handling.
func Is ¶
Is reports whether err is an *Error of the given Kind. If err is nil then Is returns false.
func MarshalError ¶
MarshalError marshals an arbitrary error and returns the byte slice. If the error is nil, it returns nil. It returns the argument slice unchanged if the error is nil. If the error is not an *Error, it just records the result of err.Error(). Otherwise it encodes the full Error struct.
func MarshalErrorAppend ¶
MarshalErrorAppend marshals an arbitrary error into a byte slice. The result is appended to b, which may be nil. It returns the argument slice unchanged if the error is nil. If the error is not an *Error, it just records the result of err.Error(). Otherwise it encodes the full Error struct.
func Match ¶
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.
For example,
Match(errors.E(errors.Op("read"), errors.Permission), err)
tests whether err is an Error with Kind=Permission and Op=read.
func Str ¶
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 Trap ¶
func Trap(values ...interface{}) error
Trap can take any function whose last returned value is an error, and return it exclusively.
func UnmarshalError ¶
UnmarshalError unmarshals the byte slice into an error value. If the slice is nil or empty, it returns nil. Otherwise the byte slice must have been created by MarshalError or MarshalErrorAppend. If the encoded error was of type *Error, the returned error value will have that underlying type. Otherwise it will be just a simple value that implements the error interface.
Types ¶
type Error ¶
type Error struct { // Op is the operation being performed, usually the name of the method // being invoked (Get, Put, etc.). It should not contain an at sign @. 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 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 (*Error) MarshalAppend ¶
MarshalAppend marshals err into a byte slice. The result is appended to b, which may be nil. It returns the argument slice unchanged if the error is nil.
func (*Error) MarshalBinary ¶
MarshalBinary marshals its receiver into a byte slice, which it returns. It returns nil if the error is nil. The returned error is always nil.
func (*Error) UnmarshalBinary ¶
UnmarshalBinary unmarshals the byte slice into the receiver, which must be non-nil. The returned error is always nil.
type Kind ¶
type Kind uint8
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 ( Other Kind = iota // Unclassified error. This value is not printed in the error message. Invalid // Invalid operation for this type of item. Permission // Permission denied. Exist // Item already exists. NotExist // Item does not exist. Internal // Internal error or inconsistency. Canceled // Canceled indicates the operation was canceled (typically by the caller). DeadlineExceeded // DeadlineExceeded means operation expired before completion. Unauthenticated // Unauthenticated indicates the request does not have valid authentication credentials for the operation. ResourceExhausted // ResourceExhausted indicates some resource has been exhausted. FailedPrecondition // FailedPrecondition indicates operation was rejected because the system is not in a state required for the operation's execution. Aborted // Aborted indicates the operation was aborted OutOfRange // OutOfRange means operation was attempted past the valid range. Unimplemented // Unimplemented indicates operation is not implemented or not supported/enabled in this service. DataLoss // DataLoss indicates unrecoverable data loss or corruption. )
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. Based on GRPC error codes.