package guru
import "zgo.at/guru"
Package guru provides Go errors with a Guru Meditation Code.
ExampleΒΆ
Code:
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 }
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 ¶
Package Files ¶
guru.go http.go
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 Errorf ¶
Errorf returns a new error message with an error code.
func HTTPUserError ¶
HTTPUserError reports if this HTTP status code is a user error (i.e. in the 4xx range).
func New ¶
New returns a new error message with an error code.
func WithCode ¶
WithCode wraps an existing error with the provided error code. It will return nil if err is nil.
func Wrap ¶
Wrap returns an error annotating err with an error code, and the supplied message. It will return nil if err is nil.
func Wrapf ¶
Wrapf returns an error annotating err with an error code, and the format specifier. It will return nil if err is nil.
- Version
- v1.1.0 (latest)
- Published
- Apr 5, 2020
- Platform
- linux/amd64
- Imports
- 2 packages (graph)
- Last checked
- 2 months ago –
Tools for package owners.