Documentation ¶
Overview ¶
Package errors implements a basic error wrapping pattern, so that errors can be annotated with additional information without losing the original error.
Example:
import "github.com/interstellar/starlight/errors" func query() error { err := pq.Exec("SELECT...") if err != nil { return errors.Wrap(err, "select query failed") } err = pq.Exec("INSERT...") if err != nil { return errors.Wrap(err, "insert query failed") } return nil } func main() { err := query() if _, ok := errors.Root(err).(sql.ErrNoRows); ok { log.Println("There were no results") return } else if err != nil { log.Println(err) return } log.Println("success") }
When to wrap errors ¶
Errors should be wrapped with additional messages when the context is ambiguous. This includes when the error could arise in multiple locations in the same function, when the error is very common and likely to appear at different points in the call tree (e.g., JSON serialization errors), or when you need specific parameters alongside the original error message.
Error handling best practices ¶
Errors are part of a function's interface. If you expect the caller to perform conditional error handling, you should document the errors returned by your function in a function comment, and include it as part of your unit tests.
Be disciplined about validating user input. Programs should draw a very clear distinction between user errors and internal errors.
Avoid redundant error logging. If you return an error, assume it will be logged higher up the call stack. For a given project, choose an appropriate layer to handle error logging.
Index ¶
- func Data(err error) map[string]interface{}
- func Detail(err error) string
- func New(text string) error
- func Root(e error) error
- func Stack(err error) *runtime.Frames
- func Sub(root, err error) error
- func WithData(err error, keyval ...interface{}) error
- func WithDetail(err error, text string) error
- func WithDetailf(err error, format string, v ...interface{}) error
- func Wrap(err error, a ...interface{}) error
- func Wrapf(err error, format string, a ...interface{}) error
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Detail ¶
Detail returns the detail message contained in err, if any. An error has a detail message if it was made by WithDetail or WithDetailf.
func Root ¶
Root returns the original error that was wrapped by one or more calls to Wrap. If e does not wrap other errors, it will be returned as-is.
func Stack ¶
Stack returns the stack trace of an error. If the error has no stack information, it returns an empty stack trace.
func Sub ¶
Sub returns an error containing root as its root and taking all other metadata (stack trace, detail, message, and data items) from err.
Sub returns nil when either root or err is nil.
Use this when you need to substitute a new root error in place of an existing error that may already hold a stack trace or other metadata.
Example ¶
package main import ( "github.com/interstellar/starlight/errors" ) var ErrInvalidKey = errors.New("invalid key") func main() { err := sign() if err != nil { err = errors.Sub(ErrInvalidKey, err) return } } func sign() error { return nil }
Output:
Example (Return) ¶
package main import ( "github.com/interstellar/starlight/errors" ) var ErrInvalidKey = errors.New("invalid key") func main() { err := sign() err = errors.Sub(ErrInvalidKey, err) return } func sign() error { return nil }
Output:
func WithData ¶
WithData returns a new error that wraps err as a chain error message containing a value of type map[string]interface{} as an extra data item. The map contains the values in the map in err, if any, plus the items in keyval. Keyval takes the form
k1, v1, k2, v2, ...
Values kN must be strings. Calling Data on the returned error yields the map. Note that if err already has a data item of any other type, it will not be accessible via the returned error value.
func WithDetail ¶
WithDetail returns a new error that wraps err as a chain error messsage containing text as its additional context. Function Detail will return the given text when called on the new error value.
func WithDetailf ¶
WithDetailf is like WithDetail, except it formats the detail message as in fmt.Printf. Function Detail will return the formatted text when called on the new error value.
Types ¶
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is in an implementation of the "sticky error writer" pattern as described in https://blog.golang.org/errors-are-values.
A Writer makes one call on the underlying writer for each call to Write, until an error is returned. From that point on, it makes no calls on the underlying writer, and returns the same error value every time.