Documentation ¶
Overview ¶
Package errors defines globaly
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 Str(text string) error
- func UnmarshalError(b []byte) error
- type Error
- type Kind
- type Op
- type PathName
- type UserName
Examples ¶
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 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. Using for minimal cominicate
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(upspin.UserName("joe@schmoe.com"), errors.Permission), err)
tests whether err is an Error with Kind=Permission and User=joe@schmoe.com.
Example ¶
package main import ( "akif/multiplayer-game-circle-tennis/errors" "fmt" ) func main() { path := errors.PathName("Jupitter") user := errors.UserName("DarkWader") err := errors.Str("network unreachable") // Construct an error, one we pretend to have received from a test. got := errors.E(errors.Op("Get"), path, user, errors.IO, err) // Now construct a reference error, which might not have all // the fields of the error from the test. expect := errors.E(user, errors.IO, err) fmt.Println("Match:", errors.Match(expect, got)) // Now one that's incorrect - wrong Kind. got = errors.E(errors.Op("Get"), path, user, errors.Permission, err) fmt.Println("Mismatch:", errors.Match(expect, got)) }
Output: Match: true Mismatch: false
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 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 { // Path is the Path PathName // User is the User UserName // Op is the operation being performed, usually the name of the method Op Op // Kind is the class of error, such as permission failure, Kind Kind // The underlying error that triggered this one, if any. Err error }
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.
Example ¶
package main import ( "akif/multiplayer-game-circle-tennis/errors" "fmt" ) func main() { path := errors.PathName("Mars") user := errors.UserName("Staller") // Single error. e1 := errors.E(errors.Op("Get"), path, errors.IO, "network unreachable") fmt.Println("\nSimple error:") fmt.Println(e1) // Nested error. fmt.Println("\nNested error:") e2 := errors.E(errors.Op("Read"), path, user, errors.Other, e1) fmt.Println(e2) }
Output: Simple error: Get: jane@doe.com/file: I/O error: network unreachable Nested error: Read: jane@doe.com/file, user joe@blow.com: I/O error: Get: network unreachable
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. IO // External I/O error such as network failure. Exist // Item already exists. NotExist // Item does not exist. IsDir // Item is a directory. NotDir // Item is not a directory. NotEmpty // Directory not empty. Private // Information withheld. Internal // Internal error or inconsistency. CannotDecrypt // No wrapped key for user with read access. Transient // A transient error. BrokenLink // Link target does not exist. )
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.