Documentation
¶
Overview ¶
Copied (and modified) from: github.com/pkg/errors@v0.9.1/stack.go
Index ¶
Constants ¶
const None = ""
Variables ¶
var ErrNone = Error{/* contains filtered or unexported fields */}
Functions ¶
func As ¶
As the sentinel type for the error.
This allows Error to be used in a switch in conjunction with linter "exhaustive".
For example:
type myError errors.Sentinel
const (
errOne = myError("one")
errTwo = myError("two")
errThree = myError("three")
)
func multipleErrors(ctx context.Context, err int) errors.Error {
switch err {
case 1:
return errors.New(ctx, errOne)
case 2:
return errors.New(ctx, errTwo)
case 3:
return errors.New(ctx, errThree)
default:
return errors.ErrNone
}
}
func TestSentinelEnum(t *testing.T) {
t.Parallel()
err := multipleErrors(context.Background(), 1)
//nolint:exhaustive // reason: expected lint error for testing
switch errors.As[myError](err) {
case errOne:
// Expected case.
default:
assert.Fail(t, "expected errOne")
}
}
Types ¶
type Error ¶ added in v0.4.0
type Error struct {
// contains filtered or unexported fields
}
Error is an instance of a sentinel error. If the error was created via New() then the error is considered and inline error and is not comparable with another Error.
Error should not be used with "==". Instead, use Error.As().
func Catch ¶ added in v0.2.0
Catch potential panics that occur in a function call. Panics should never occur, therefore stack traces print regardless of build mode (release or debug).
func New ¶
New error instance.
This should be called when the application creates a new error. This new Error is not (intended to be) comparable with other Errors and therefore cannot be used as a sentinel error. A Sentinel may be used and compared using Error.Sentinel(). A context is accepted in order to pass through API level feature flags.