Documentation ¶
Overview ¶
Package guru provides Go errors with a Guru Meditation Code.
Example ¶
package main import ( "errors" "fmt" "net/http" "zgo.at/guru" ) // Error constants. const ( CodeFruitOverflow = iota + 1 CodeBoozeUnderrun CodeExpired ) func main() { // Construct a new error. err := guru.New(CodeFruitOverflow, "too many bananas") fmt.Println(err) // error 1: too many bananas // Retrieve the error code code := guru.Code(err) fmt.Println(code) // 1 // Add error code to existing error. err = errors.New("not enough beer") err = guru.WithCode(CodeBoozeUnderrun, err) fmt.Println(err) // error 2: not enough beer // Add error code to existing error with context. err = errors.New("Dennis Ritchie") err = guru.Wrap(CodeExpired, err, "no longer with us") fmt.Println(err) // error 3: Dennis Ritchie: no longer with us // For HTTP applications, it may be useful to directly the HTTP status codes: err = guru.New(http.StatusNotAcceptable, "Justin Bieber") fmt.Println(err) // error 406: Justin Bieber // Error codes can be overriden: err = guru.New(1, "oh noes") err = guru.WithCode(2, err) fmt.Println(guru.Code(err)) // 2 }
Output:
Index ¶
- func Code(err error) int
- func Errorf(code int, format string, args ...interface{}) error
- func HTTPUserError(err error) bool
- func New(code int, msg string) error
- func WithCode(code int, err error) error
- func Wrap(code int, err error, msg string) error
- func Wrapf(code int, err error, msg string, args ...interface{}) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Code ¶
Code extracts the highest-level error code from the error or the errors it wraps. It will return 0 if the error does not implement the coder interface.
func HTTPUserError ¶
HTTPUserError reports if this HTTP status code is a user error (i.e. in the 4xx range).
func WithCode ¶
WithCode wraps an existing error with the provided error code. It will return nil if err is nil.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.