errors

package module
v0.0.0-...-ebbb2e5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2017 License: MIT Imports: 6 Imported by: 1

README

errors GoDoc

Similar to standard library errors but with some stack trace goodness.

Usage

err := errors.New("whoops!") // looks familiar?

You can even wrap an error to provide context.

_, err := w.Write(p)
if err != nil {
    err = errors.Wrap(err, "Example failed")
}
fmt.Println(err) // Example failed. write error [github.com/alexkappa/errors.Wrap(errors.go:76),github.com/alexkappa/errors.ExampleWrap(errors_test.go:56),testing.runExample(example.go:99),testing.RunExamples(example.go:36),testing.(*M).Run(testing.go:486),main.main(_testmain.go:58)]

You can also access the stack trace and print it out yourself.

err := errors.New("error with stack trace")
for _, frame := range err.Stack() {
    fmt.Printf("%s\n", frame.Func)
}
// github.com/alexkappa/errors.New
// main.main

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

func Errorf(message string, args ...interface{}) Error

Errorf creates a new Error with the supplied message and arguments formatted in the manner of fmt.Printf.

func New

func New(message string) Error

New creates a new Error with the supplied message.

func Wrap

func Wrap(err error, message string) Error

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

func Wrapf

func Wrapf(err error, message string, args ...interface{}) Error

Wrapf creates a new Error that wraps err formatted in the manner of fmt.Printf.

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

func Stack(skip int) Frames

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

func (f Frames) MarshalJSON() ([]byte, error)

func (Frames) String

func (f Frames) String() string

String is used to satisfy the fmt.Stringer interface. It formats the stack trace as a comma separated list of "file:line function".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL