Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ An error *leaf* is an object that implements the `error` interface,
but does not refer to another error via a `Unwrap()` or `Cause()`
method.

- `New(string) error`, `Newf(string, ...interface{}) error`, `Errorf(string, ...interface{}) error`: leaf errors with message
- `New(string) error`, `Newf(string, ...any) error`, `Errorf(string, ...any) error`: leaf errors with message
- **when to use: common error cases.**
- what it does: also captures the stack trace at point of call and redacts the provided message for safe reporting.
- how to access the detail: `Error()`, regular Go formatting. **Details in Sentry report.**
- see also: Section [Error composition](#Error-composition-summary) below. `errors.NewWithDepth()` variants to customize at which call depth the stack trace is captured.

- `AssertionFailedf(string, ...interface{}) error`, `NewAssertionFailureWithWrappedErrf(error, string, ...interface{}) error`: signals an assertion failure / programming error.
- `AssertionFailedf(string, ...any) error`, `NewAssertionFailureWithWrappedErrf(error, string, ...any) error`: signals an assertion failure / programming error.
- **when to use: when an invariant is violated; when an unreachable code path is reached.**
- what it does: also captures the stack trace at point of call, redacts the provided strings for safe reporting, prepares a hint to inform a human user.
- how to access the detail: `IsAssertionFailure()`/`HasAssertionFailure()`, format with `%+v`, Safe details included in Sentry reports.
Expand Down Expand Up @@ -143,7 +143,7 @@ they behave as no-ops in this case:
return errors.Wrap(foo(), "foo")
```

- `Wrap(error, string) error`, `Wrapf(error, string, ...interface{}) error`:
- `Wrap(error, string) error`, `Wrapf(error, string, ...any) error`:
- **when to use: on error return paths.**
- what it does: combines `WithMessage()`, `WithStack()`, `WithSafeDetails()`.
- how to access the details: `Error()`, regular Go formatting. **Details in Sentry report.**
Expand Down Expand Up @@ -198,22 +198,22 @@ return errors.Wrap(foo(), "foo")
- how to access the details: format with `%+v`, `errors.GetSafeDetails()`, Sentry reports. The stack trace is considered safe for reporting.
- see also: `WithStackDepth()` to customize the call depth at which the stack trace is captured.

- `WithSafeDetails(error, string, ...interface{}) error`: safe details for reporting.
- `WithSafeDetails(error, string, ...any) error`: safe details for reporting.
- when to use: probably never. Use `errors.Wrap()`/`errors.Wrapf()` instead.
- what it does: saves some strings for safe reporting.
- how to access the detail: format with `%+v`, `errors.GetSafeDetails()`, Sentry report.

- `WithMessage(error, string) error`, `WithMessagef(error, string, ...interface{}) error`: message prefix.
- `WithMessage(error, string) error`, `WithMessagef(error, string, ...any) error`: message prefix.
- when to use: probably never. Use `errors.Wrap()`/`errors.Wrapf()` instead.
- what it does: adds a message prefix.
- how to access the detail: `Error()`, regular Go formatting, Sentry Report.

- `WithDetail(error, string) error`, `WithDetailf(error, string, ...interface{}) error`, user-facing detail with contextual information.
- `WithDetail(error, string) error`, `WithDetailf(error, string, ...any) error`, user-facing detail with contextual information.
- **when to use: need to embark a message string to output when the error is presented to a developer.**
- what it does: captures detail strings.
- how to access the detail: `errors.GetAllDetails()`, `errors.FlattenDetails()` (all details are preserved), format with `%+v`. Not included in Sentry reports.

- `WithHint(error, string) error`, `WithHintf(error, string, ...interface{}) error`: user-facing detail with suggestion for action to take.
- `WithHint(error, string) error`, `WithHintf(error, string, ...any) error`: user-facing detail with suggestion for action to take.
- **when to use: need to embark a message string to output when the error is presented to an end user.**
- what it does: captures hint strings.
- how to access the detail: `errors.GetAllHints()`, `errors.FlattenHints()` (hints are de-duplicated), format with `%+v`. Not included in Sentry reports.
Expand Down Expand Up @@ -279,7 +279,7 @@ It is possible to opt additional in to Sentry reporting, using either of the fol
- it also makes it available via `errors.GetSafeDetails()`/`GetAllSafeDetails()`.
- the value 123 is also part of the main error message returned by `Error()`.

- attach additional arbitrary strings with `errors.WithSafeDetails(error, string, ...interface{}) error` and
- attach additional arbitrary strings with `errors.WithSafeDetails(error, string, ...any) error` and
also use `errors.Safe()`.
For example: `err = errors.WithSafeDetails(err, "additional data: %s", errors.Safe("hello"))`.
- in this example, the string "hello" will be included in Sentry reports.
Expand Down Expand Up @@ -561,8 +561,8 @@ func Formattable(err error) fmt.Formatter
// Identify errors.
func Is(err, reference error) bool
func IsAny(err error, references ...error) bool
func If(err error, pred func(err error) (interface{}, bool)) (interface{}, bool)
func As(err error, target interface{}) bool
func If(err error, pred func(err error) (any, bool)) (any, bool)
func As(err error, target any) bool

// Encode/decode errors.
type EncodedError // this is protobuf-encodable
Expand All @@ -589,7 +589,7 @@ type MultiCauseDecoder = func(ctx context.Context, causes []error, msgPrefix str
func RegisterTypeMigration(previousPkgPath, previousTypeName string, newType error)

// Sentry reports.
func BuildSentryReport(err error) (*sentry.Event, map[string]interface{})
func BuildSentryReport(err error) (*sentry.Event, map[string]any)
func ReportError(err error) (string)

// Stack trace captures.
Expand All @@ -604,10 +604,10 @@ func GetSafeDetails(err error) (payload SafeDetailPayload)

// Obsolete APIs.
type SafeMessager interface { ... }
func Redact(r interface{}) string
func Redact(r any) string

// Aliases redact.Safe.
func Safe(v interface{}) SafeMessager
func Safe(v any) SafeMessager

// Assertion failures.
func HasAssertionFailure(err error) bool
Expand Down
2 changes: 1 addition & 1 deletion assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func WithAssertionFailure(err error) error {
// HasAssertionFailure returns true if the error or any of its causes
// is an assertion failure annotation.
func HasAssertionFailure(err error) bool {
_, ok := markers.If(err, func(err error) (v interface{}, ok bool) {
_, ok := markers.If(err, func(err error) (v any, ok bool) {
v, ok = err.(*withAssertionFailure)
return
})
Expand Down
4 changes: 2 additions & 2 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAssert(t *testing.T) {

tt.Check(assert.HasAssertionFailure(err))

if _, ok := markers.If(err, func(err error) (interface{}, bool) { return nil, assert.IsAssertionFailure(err) }); !ok {
if _, ok := markers.If(err, func(err error) (any, bool) { return nil, assert.IsAssertionFailure(err) }); !ok {
t.Error("woops")
}

Expand All @@ -51,7 +51,7 @@ func TestAssert(t *testing.T) {

tt.Check(assert.HasAssertionFailure(newErr))

if _, ok := markers.If(newErr, func(err error) (interface{}, bool) { return nil, assert.IsAssertionFailure(err) }); !ok {
if _, ok := markers.If(newErr, func(err error) (any, bool) { return nil, assert.IsAssertionFailure(err) }); !ok {
t.Error("woops")
}

Expand Down
2 changes: 1 addition & 1 deletion barriers/barriers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func HandledWithSafeMessage(err error, msg redact.RedactableString) error {

// HandledWithMessagef is like HandledWithMessagef except the message
// is formatted.
func HandledWithMessagef(err error, format string, args ...interface{}) error {
func HandledWithMessagef(err error, format string, args ...any) error {
if err == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion contexttags/contexttags.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func redactableTagsIterate(b *logtags.Buffer, fn func(i int, s redact.Redactable
k := t.Key()
v := t.Value()
eq := empty
var val interface{} = empty
var val any = empty
if v != nil {
if len(k) > 1 {
eq = "="
Expand Down
4 changes: 2 additions & 2 deletions errbase/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func encodeLeaf(ctx context.Context, err error, causes []error) EncodedError {

// warningFn can be overridden with a suitable logging function using
// SetWarningFn() below.
var warningFn = func(_ context.Context, format string, args ...interface{}) {
var warningFn = func(_ context.Context, format string, args ...any) {
log.Printf(format, args...)
}

// SetWarningFn enables configuration of the warning function.
func SetWarningFn(fn func(context.Context, string, ...interface{})) {
func SetWarningFn(fn func(context.Context, string, ...any)) {
warningFn = fn
}

Expand Down
12 changes: 6 additions & 6 deletions errbase/format_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,17 +871,17 @@ func (s *printer) Detail() bool {
return ((*state)(s)).detail()
}

func (s *printer) Print(args ...interface{}) {
func (s *printer) Print(args ...any) {
s.enhanceArgs(args)
fmt.Fprint((*state)(s), args...)
}

func (s *printer) Printf(format string, args ...interface{}) {
func (s *printer) Printf(format string, args ...any) {
s.enhanceArgs(args)
fmt.Fprintf((*state)(s), format, args...)
}

func (s *printer) enhanceArgs(args []interface{}) {
func (s *printer) enhanceArgs(args []any) {
prevStack := s.lastStack
lastSeen := prevStack
for i := range args {
Expand All @@ -908,17 +908,17 @@ func (s *safePrinter) Detail() bool {
return ((*state)(s)).detail()
}

func (s *safePrinter) Print(args ...interface{}) {
func (s *safePrinter) Print(args ...any) {
s.enhanceArgs(args)
redact.Fprint((*state)(s), args...)
}

func (s *safePrinter) Printf(format string, args ...interface{}) {
func (s *safePrinter) Printf(format string, args ...any) {
s.enhanceArgs(args)
redact.Fprintf((*state)(s), format, args...)
}

func (s *safePrinter) enhanceArgs(args []interface{}) {
func (s *safePrinter) enhanceArgs(args []any) {
prevStack := s.lastStack
lastSeen := prevStack
for i := range args {
Expand Down
4 changes: 2 additions & 2 deletions errbase/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ type SafeFormatter interface {
// typically provide their own implementations.
type Printer interface {
// Print appends args to the message output.
Print(args ...interface{})
Print(args ...any)

// Printf writes a formatted string.
Printf(format string, args ...interface{})
Printf(format string, args ...any)

// Detail reports whether error detail is requested.
// After the first call to Detail, all text written to the Printer
Expand Down
2 changes: 1 addition & 1 deletion errbase/stack_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Error types: (1) *errors.withStack (2) *errors.fundamental`)
})
}

func fmtClean(x interface{}) string {
func fmtClean(x any) string {
spv := fmt.Sprintf("%+v", x)
spv = fileref.ReplaceAllString(spv, "<path>:<lineno>")
spv = strings.ReplaceAll(spv, "\t", "<tab>")
Expand Down
2 changes: 1 addition & 1 deletion errbase_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func RegisterMultiCauseEncoder(typeName TypeKey, encoder MultiCauseEncoder) {
type MultiCauseEncoder = errbase.MultiCauseEncoder

// SetWarningFn enables configuration of the warning function.
func SetWarningFn(fn func(context.Context, string, ...interface{})) { errbase.SetWarningFn(fn) }
func SetWarningFn(fn func(context.Context, string, ...any)) { errbase.SetWarningFn(fn) }

// A Formatter formats error messages.
//
Expand Down
6 changes: 3 additions & 3 deletions errutil/as.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// As finds the first error in err's chain that matches the type to which target
// points, and if so, sets the target to its value and returns true. An error
// matches a type if it is assignable to the target type, or if it has a method
// As(interface{}) bool such that As(target) returns true. As will panic if target
// As(any) bool such that As(target) returns true. As will panic if target
// is not a non-nil pointer to a type which implements error or is of interface type.
//
// The As method should set the target to its value and return true if err
Expand All @@ -33,7 +33,7 @@ import (
// Note: this implementation differs from that of xerrors as follows:
// - it also supports recursing through causes with Cause().
// - if it detects an API use error, its panic object is a valid error.
func As(err error, target interface{}) bool {
func As(err error, target any) bool {
if target == nil {
panic(AssertionFailedf("errors.As: target cannot be nil"))
}
Expand All @@ -55,7 +55,7 @@ func As(err error, target interface{}) bool {
val.Elem().Set(reflect.ValueOf(c))
return true
}
if x, ok := c.(interface{ As(interface{}) bool }); ok && x.As(target) {
if x, ok := c.(interface{ As(any) bool }); ok && x.As(target) {
return true
}

Expand Down
8 changes: 4 additions & 4 deletions errutil/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
// - via `errors.GetSafeDetails()`, shows redacted strings.
// - when formatting with `%+v`.
// - in Sentry reports.
func AssertionFailedf(format string, args ...interface{}) error {
func AssertionFailedf(format string, args ...any) error {
return AssertionFailedWithDepthf(1, format, args...)
}

// AssertionFailedWithDepthf creates an internal error
// with a stack trace collected at the specified depth.
// See the doc of `AssertionFailedf()` for more details.
func AssertionFailedWithDepthf(depth int, format string, args ...interface{}) error {
func AssertionFailedWithDepthf(depth int, format string, args ...any) error {
err := NewWithDepthf(1+depth, format, args...)
err = assert.WithAssertionFailure(err)
return err
Expand Down Expand Up @@ -62,7 +62,7 @@ func HandleAsAssertionFailureDepth(depth int, origErr error) error {
// context of the caller are preserved. The original error is not
// visible as cause any more. The original error message is preserved.
// See the doc of `AssertionFailedf()` for more details.
func NewAssertionErrorWithWrappedErrf(origErr error, format string, args ...interface{}) error {
func NewAssertionErrorWithWrappedErrf(origErr error, format string, args ...any) error {
return NewAssertionErrorWithWrappedErrDepthf(1, origErr, format, args...)
}

Expand All @@ -71,7 +71,7 @@ func NewAssertionErrorWithWrappedErrf(origErr error, format string, args ...inte
// stack is captured can be specified.
// See the doc of `AssertionFailedf()` for more details.
func NewAssertionErrorWithWrappedErrDepthf(
depth int, origErr error, format string, args ...interface{},
depth int, origErr error, format string, args ...any,
) error {
err := barriers.Handled(origErr)
err = WrapWithDepthf(depth+1, err, format, args...)
Expand Down
2 changes: 1 addition & 1 deletion errutil/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func WithMessage(err error, message string) error {
// If err is nil, WithMessagef returns nil.
// The message is formatted as per redact.Sprintf,
// to separate safe and unsafe strings for Sentry reporting.
func WithMessagef(err error, format string, args ...interface{}) error {
func WithMessagef(err error, format string, args ...any) error {
if err == nil {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions errutil/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ func NewWithDepth(depth int, msg string) error {
// strings that may contain PII information.
//
// See the doc of `New()` for more details.
func Newf(format string, args ...interface{}) error {
func Newf(format string, args ...any) error {
return NewWithDepthf(1, format, args...)
}

// NewWithDepthf is like Newf() except the depth to capture the stack
// trace is configurable.
// See the doc of `New()` for more details.
func NewWithDepthf(depth int, format string, args ...interface{}) error {
func NewWithDepthf(depth int, format string, args ...any) error {
// If there's the verb %w in here, shortcut to fmt.Errorf()
// and store the safe details as extra payload. That's
// because we don't want to re-implement the error wrapping
Expand Down Expand Up @@ -133,14 +133,14 @@ func WrapWithDepth(depth int, err error, msg string) error {
// - everything when formatting with `%+v`.
// - stack trace, format, and redacted details via `errors.GetSafeDetails()`.
// - stack trace, format, and redacted details in Sentry reports.
func Wrapf(err error, format string, args ...interface{}) error {
func Wrapf(err error, format string, args ...any) error {
return WrapWithDepthf(1, err, format, args...)
}

// WrapWithDepthf is like Wrapf except the depth to capture the stack
// trace is configurable.
// The the doc of `Wrapf()` for more details.
func WrapWithDepthf(depth int, err error, format string, args ...interface{}) error {
func WrapWithDepthf(depth int, err error, format string, args ...any) error {
if err == nil {
return nil
}
Expand Down
Loading