Documentation
¶
Overview ¶
Core Errors ¶
To create your core errors, use Error. For example:
const (
ErrNilReceiver = erorr.Error("nil receiver")
ErrNotFound = erorr.Error("not found")
ErrUndefined = erorr.Error("undefined")
)
Note that you can create errors with Error using `const` (rather than `var`).
Error Annotations ¶
Use Wrap to annotate your core errors and other errors.
if nil != err {
return erorr.Wrap(err, "API request failed.",
field.String("request-uri", requestURI),
field.String("service", "monitor"),
)
}
Note that error annotations of this type automatically contains a call-trace.
You can also annotate multiple errors:
var errs []error
// ...
return erorr.Wrap(erorr.Errors(errs), "API request failed.",
field.String("request-uri", requestURI),
field.String("service", "monitor"),
)
Contextual Errors ¶
Use Stamp to create a contextual error.
if nil != err {
return erorr.Wrap("API request failed.",
field.String("request-uri", requestURI),
field.String("service", "monitor"),
)
}
Note that Stamp is similar to Wrap, with the difference being that, Stamp does not wrap another error.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Errorf ¶
Errorf returns a formatted error.
If an one or more errors are passed in the parameters of Errorf, then they are wrapped. If there is one error, then a WrappedError is returned. If there is more-than one error, then a WrappedErrors is returned. If there are no errors, then a StampedError is returned.
Example (OneError) ¶
const ErrFailure = erorr.Error("failure!")
// ...
var err error = ErrFailure
service := "AuthService"
err = erorr.Errorf("Uh oh, something bad happened to %s: %w", service, err) // <---------
fmt.Printf("error-message: %s\n", err)
fmt.Printf("errors-type: %T\n", err)
Output: error-message: Uh oh, something bad happened to AuthService: failure! errors-type: erorr.WrappedError
Example (TwoErrors) ¶
const ErrFailure = erorr.Error("failure!")
const ErrProblem = erorr.Error("problem!")
// ...
service := "AuthService"
err := erorr.Errorf("Uh oh, some bad things happened to %s: %w and %w", service, ErrFailure, ErrProblem) // <---------
fmt.Printf("error-message: %s\n", err)
fmt.Printf("errors-type: %T\n", err)
Output: error-message: Uh oh, some bad things happened to AuthService: failure! and problem! errors-type: erorr.WrappedErrors
func Is ¶
Is returns true if the error matches the target, and returns false otherwise.
An error matches the target if the error equals the target, or if any wrapped errors (inside of the error) equals the target.
Example usage:
if erorr.Is(err, io.EOF) {
// ...
}
func Stamp ¶
Stamp returns a StampedError.
If you are not sure whetherto use StampError or Stamp use Stamp.
Types ¶
type Error ¶
type Error string
Error is a simple error type.
Error is implemented in a way such that you can create `const` errors.
For example:
const ErrNilReceiver = erorr.Error("nil receiver")
Use Error for your core errors.
type StampedError ¶
type StampedError struct {
// contains filtered or unexported fields
}
StampedError is contextual error.
When a StampedError is created includes a call-trace, and a list of fields.
For example:
err := erorr.Stamp("client is nil",
field.String("url", url),
)
func StampError ¶
func StampError(msg string, fields ...field.Field[string]) StampedError
StampError returns a StampedError.
If you are not sure whetherto use StampError or Stamp use Stamp.
func (StampedError) CallTrace ¶
func (receiver StampedError) CallTrace() string
func (StampedError) Error ¶
func (receiver StampedError) Error() string
type WrappedError ¶
type WrappedError struct {
// contains filtered or unexported fields
}
func (WrappedError) CallTrace ¶
func (receiver WrappedError) CallTrace() string
func (WrappedError) Error ¶
func (receiver WrappedError) Error() string
func (WrappedError) Unwrap ¶
func (receiver WrappedError) Unwrap() error
type WrappedErrors ¶
type WrappedErrors struct {
// contains filtered or unexported fields
}
func WrapErrors ¶
func (WrappedErrors) CallTrace ¶
func (receiver WrappedErrors) CallTrace() string
func (WrappedErrors) Error ¶
func (receiver WrappedErrors) Error() string
func (WrappedErrors) Unwrap ¶
func (receiver WrappedErrors) Unwrap() []error