Documentation ¶
Overview ¶
Package wrap provides utility functions to wrap errors with extra context in an easy-to-read format.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error wraps the given error with a message for context.
The error is displayed on the following format:
err := errors.New("expired token") wrapped := wrap.Error(err, "user authentication failed") fmt.Println(wrapped) // user authentication failed // - expired token
Wrapped errors can be nested. Wrapping an already wrapped error adds it to the error list, as follows:
err := errors.New("expired token") inner := wrap.Error(err, "user authentication failed") outer := wrap.Error(inner, "failed to update username") fmt.Println(outer) // failed to update username // - user authentication failed // - expired token
The returned error implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
func Errorf ¶
Errorf wraps the given error with a message for context. It forwards the given message format and args to fmt.Sprintf to construct the message.
Example:
err := errors.New("username already taken") wrapped := wrap.Errorf(err, "failed to create user with name '%s'", "hermannm") fmt.Println(wrapped) // failed to create user with name 'hermannm' // - username already taken
func Errors ¶
Errors wraps the given errors with a message for context.
The error is displayed on the following format:
err1 := errors.New("username too long") err2 := errors.New("invalid email") wrapped := wrap.Errors("user creation failed", err1, err2) fmt.Println(wrapped) // user creation failed // - username too long // - invalid email
When combined with Error, nested wrapped errors are indented as follows:
err1 := errors.New("username too long") err2 := errors.New("invalid email") inner := wrap.Errors("user creation failed", err1, err2) outer := wrap.Error(inner, "failed to register new user") fmt.Println(outer) // failed to register new user // - user creation failed // - username too long // - invalid email
The returned error implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
Types ¶
This section is empty.