Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // This setting enables a stack trace to be printed when an Error is being // marshaled. // // err := errors.New("example") // b, _ := json.Marshal(err) // {"message":"example","stack":[{"file":"<file>","line":<line>,"func": "<function>"},...]} MarshalTrace = false )
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error interface { // Message returns the error message of the error. Message() string // Inner returns the inner error that this error wraps. Inner() error // Stack returns the stack trace that led to the error. Stack() Frames error }
Error is an interface that extends the builtin error interface with inner errors and stack traces.
func Errorf ¶
Errorf creates a new Error with the supplied message and arguments formatted in the manner of fmt.Printf.
func Wrap ¶
Wrap creates a new Error that wraps err.
Example ¶
package main import ( "fmt" "github.com/alexkappa/errors" ) type errWriter uint8 func (e errWriter) Write(p []byte) (int, error) { return 0, fmt.Errorf("write error") } var ( w errWriter p []byte ) func main() { _, err := w.Write(p) if err != nil { err = errors.Wrap(err, "Example failed") } fmt.Println(err) }
Output: Example failed. write error
type Frame ¶
type Frame struct { // File is the path to the file of the caller. File string `json:"file"` // Line is the line in the file where the function call was made. Line int `json:"line"` // Func is the name of the caller. Func string `json:"func"` }
Frame contains information for a single stack frame.
type Frames ¶
type Frames []Frame
func Stack ¶
Stack returns the stack trace of the function call, while skipping the first skip frames.
Example ¶
package main import ( "fmt" "github.com/alexkappa/errors" ) func main() { err := errors.New("error with stack trace") for _, frame := range err.Stack() { fmt.Printf("%s(%s:%d)\n", frame.Func, frame.File, 0) } }
Output: github.com/alexkappa/errors.New(errors.go:0) github.com/alexkappa/errors_test.ExampleStack(examples_test.go:0) testing.runExample(example.go:0) testing.runExamples(example.go:0) testing.(*M).Run(testing.go:0) main.main(_testmain.go:0)
func (Frames) MarshalJSON ¶
Click to show internal directories.
Click to hide internal directories.