Skip to content

Commit 7540597

Browse files
committedAug 21, 2020
Add top-level API comments.
1 parent 3c5c4ca commit 7540597

19 files changed

+563
-117
lines changed
 

‎assert_api.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@ package errors
1616

1717
import "github.com/cockroachdb/errors/assert"
1818

19-
// WithAssertionFailure forwards a definition.
19+
// WithAssertionFailure decorates the error with an assertion failure marker.
20+
// This is not intended to be used directly (see AssertionFailed() for
21+
// further decoration).
22+
//
23+
// Detail is shown:
24+
// - when formatting with `%+v`.
25+
// - in Sentry reports.
2026
func WithAssertionFailure(err error) error { return assert.WithAssertionFailure(err) }
2127

22-
// HasAssertionFailure forwards a definition.
28+
// HasAssertionFailure returns true if the error or any of its causes
29+
// is an assertion failure annotation.
2330
func HasAssertionFailure(err error) bool { return assert.HasAssertionFailure(err) }
2431

25-
// IsAssertionFailure forwards a definition.
32+
// IsAssertionFailure returns true if the error (not its causes) is an
33+
// assertion failure annotation. Consider using markers.If or
34+
// HasAssertionFailure to test both the error and its causes.
2635
func IsAssertionFailure(err error) bool { return assert.IsAssertionFailure(err) }

‎barriers_api.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@ package errors
1616

1717
import "github.com/cockroachdb/errors/barriers"
1818

19-
// Handled forwards a definition.
19+
// Handled swallows the provided error and hides it from the
20+
// Cause()/Unwrap() interface, and thus the Is() facility that
21+
// identifies causes. However, it retains it for the purpose of
22+
// printing the error out (e.g. for troubleshooting). The error
23+
// message is preserved in full.
24+
//
25+
// Detail is shown:
26+
// - via `errors.GetSafeDetails()`, shows details from hidden error.
27+
// - when formatting with `%+v`.
28+
// - in Sentry reports.
2029
func Handled(err error) error { return barriers.Handled(err) }
2130

22-
// HandledWithMessage forwards a definition.
31+
// HandledWithMessage is like Handled except the message is overridden.
32+
// This can be used e.g. to hide message details or to prevent
33+
// downstream code to make assertions on the message's contents.
2334
func HandledWithMessage(err error, msg string) error { return barriers.HandledWithMessage(err, msg) }

‎contexttags_api.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,27 @@ import (
2121
"github.com/cockroachdb/logtags"
2222
)
2323

24-
// WithContextTags forwards a definition.
24+
// WithContextTags captures the k/v pairs stored in the context via the
25+
// `logtags` package and annotates them on the error.
26+
//
27+
// Only the stromg representation of values remains available. This is
28+
// because the library cannot guarantee that the underlying value is
29+
// preserved across the network. To avoid creating a stateful interface
30+
// (where the user code needs to know whether an error has traveled
31+
// through the network or not), the library restricts access to the
32+
// value part as strings. See GetContextTags() below.
33+
//
34+
// Detail is shown:
35+
// - via `errors.GetSafeDetails()`.
36+
// - via `GetContextTags()` below.
37+
// - when formatting with `%+v`.
38+
// - in Sentry reports.
2539
func WithContextTags(err error, ctx context.Context) error {
2640
return contexttags.WithContextTags(err, ctx)
2741
}
2842

29-
// GetContextTags forwards a definition.
43+
// GetContextTags retrieves the k/v pairs stored in the error.
44+
// The sets are returned from outermost to innermost level of cause.
45+
// The returned logtags.Buffer only know about the string
46+
// representation of the values originally captured by the error.
3047
func GetContextTags(err error) []*logtags.Buffer { return contexttags.GetContextTags(err) }

‎domains/domains.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func PackageDomain() Domain {
144144
}
145145

146146
// PackageDomainAtDepth returns an error domain that describes the
147-
// package at the given depth.
147+
// package at the given call depth.
148148
func PackageDomainAtDepth(depth int) Domain {
149149
_, f, _, _ := runtime.Caller(1 + depth)
150150
return Domain("error domain: pkg " + filepath.Dir(f))

‎domains_api.go

+33-12
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,63 @@ package errors
1616

1717
import "github.com/cockroachdb/errors/domains"
1818

19-
// Domain forwards a definition.
19+
// Domain is the type of a domain annotation.
2020
type Domain = domains.Domain
2121

22-
// NoDomain forwards a definition.
22+
// NoDomain is the domain of errors that don't originate
23+
// from a barrier.
2324
const NoDomain Domain = domains.NoDomain
2425

25-
// NamedDomain forwards a definition.
26+
// NamedDomain returns an error domain identified by the given string.
2627
func NamedDomain(domainName string) Domain { return domains.NamedDomain(domainName) }
2728

28-
// PackageDomain forwards a definition.
29+
// PackageDomain returns an error domain that represents the
30+
// package of its caller.
2931
func PackageDomain() Domain { return domains.PackageDomainAtDepth(1) }
3032

31-
// PackageDomainAtDepth forwards a definition.
33+
// PackageDomainAtDepth returns an error domain that describes the
34+
// package at the given call depth.
3235
func PackageDomainAtDepth(depth int) Domain { return domains.PackageDomainAtDepth(depth) }
3336

34-
// WithDomain forwards a definition.
37+
// WithDomain wraps an error so that it appears to come from the given domain.
38+
//
39+
// Domain is shown:
40+
// - via `errors.GetSafeDetails()`.
41+
// - when formatting with `%+v`.
42+
// - in Sentry reports.
3543
func WithDomain(err error, domain Domain) error { return domains.WithDomain(err, domain) }
3644

37-
// NotInDomain forwards a definition.
45+
// NotInDomain returns true if and only if the error's
46+
// domain is not one of the specified domains.
3847
func NotInDomain(err error, doms ...Domain) bool { return domains.NotInDomain(err, doms...) }
3948

40-
// EnsureNotInDomain forwards a definition.
49+
// EnsureNotInDomain checks whether the error is in the given domain(s).
50+
// If it is, the given constructor if provided is called to construct
51+
// an alternate error. If no error constructor is provided,
52+
// a new barrier is constructed automatically using the first
53+
// provided domain as new domain. The original error message
54+
// is preserved.
4155
func EnsureNotInDomain(err error, constructor DomainOverrideFn, forbiddenDomains ...Domain) error {
4256
return domains.EnsureNotInDomain(err, constructor, forbiddenDomains...)
4357
}
4458

45-
// DomainOverrideFn forwards a definition.
59+
// DomainOverrideFn is the type of the callback function passed to EnsureNotInDomain().
4660
type DomainOverrideFn = func(originalDomain Domain, err error) error
4761

48-
// HandledInDomain forwards a definition.
62+
// HandledInDomain creates an error in the given domain and retains
63+
// the details of the given original error as context for
64+
// debugging. The original error is hidden and does not become a
65+
// "cause" for the new error. The original's error _message_
66+
// is preserved.
67+
//
68+
// See the documentation of `WithDomain()` and `errors.Handled()` for details.
4969
func HandledInDomain(err error, domain Domain) error { return domains.HandledInDomain(err, domain) }
5070

51-
// HandledInDomainWithMessage forwards a definition.
71+
// HandledInDomainWithMessage is like HandledWithMessage but with a domain.
5272
func HandledInDomainWithMessage(err error, domain Domain, msg string) error {
5373
return domains.HandledInDomainWithMessage(err, domain, msg)
5474
}
5575

56-
// GetDomain forwards a definition.
76+
// GetDomain extracts the domain of the given error, or NoDomain if
77+
// the error's cause does not have a domain annotation.
5778
func GetDomain(err error) Domain { return domains.GetDomain(err) }

‎errbase/format_error.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
)
3737

3838
// FormatError formats an error according to s and verb.
39+
// This is a helper meant for use when implementing the fmt.Formatter
40+
// interface on custom error objects.
3941
//
4042
// If the error implements errors.Formatter, FormatError calls its
4143
// FormatError method of f with an errors.Printer configured according

‎errbase_api.go

+99-30
Original file line numberDiff line numberDiff line change
@@ -21,90 +21,159 @@ import (
2121
"github.com/cockroachdb/errors/errbase"
2222
)
2323

24-
// UnwrapOnce forwards a definition.
24+
// UnwrapOnce accesses the direct cause of the error if any, otherwise
25+
// returns nil.
26+
//
27+
// It supports both errors implementing causer (`Cause()` method, from
28+
// github.com/pkg/errors) and `Wrapper` (`Unwrap()` method, from the
29+
// Go 2 error proposal).
2530
func UnwrapOnce(err error) error { return errbase.UnwrapOnce(err) }
2631

27-
// UnwrapAll forwards a definition.
32+
// UnwrapAll accesses the root cause object of the error.
33+
// If the error has no cause (leaf error), it is returned directly.
2834
func UnwrapAll(err error) error { return errbase.UnwrapAll(err) }
2935

30-
// EncodedError forwards a definition.
36+
// EncodedError is the type of an encoded (and protobuf-encodable) error.
3137
type EncodedError = errbase.EncodedError
3238

33-
// EncodeError forwards a definition.
39+
// EncodeError encodes an error.
3440
func EncodeError(ctx context.Context, err error) EncodedError { return errbase.EncodeError(ctx, err) }
3541

36-
// DecodeError forwards a definition.
42+
// DecodeError decodes an error.
3743
func DecodeError(ctx context.Context, enc EncodedError) error { return errbase.DecodeError(ctx, enc) }
3844

39-
// SafeDetailer forwards a definition.
45+
// SafeDetailer is an interface that can be implemented by errors that
46+
// can provide PII-free additional strings suitable for reporting or
47+
// telemetry.
4048
type SafeDetailer = errbase.SafeDetailer
4149

42-
// GetAllSafeDetails forwards a definition.
50+
// GetAllSafeDetails collects the safe details from the given error object
51+
// and all its causes.
52+
// The details are collected from outermost to innermost level of cause.
4353
func GetAllSafeDetails(err error) []SafeDetailPayload { return errbase.GetAllSafeDetails(err) }
4454

45-
// GetSafeDetails forwards a definition.
55+
// GetSafeDetails collects the safe details from the given error
56+
// object. If it is a wrapper, only the details from the wrapper are
57+
// returned.
4658
func GetSafeDetails(err error) (payload SafeDetailPayload) { return errbase.GetSafeDetails(err) }
4759

48-
// SafeDetailPayload forwards a definition.
60+
// SafeDetailPayload captures the safe strings for one
61+
// level of wrapping.
4962
type SafeDetailPayload = errbase.SafeDetailPayload
5063

51-
// RegisterLeafDecoder forwards a definition.
64+
// RegisterLeafDecoder can be used to register new leaf error types to
65+
// the library. Registered types will be decoded using their own
66+
// Go type when an error is decoded. Wrappers that have not been
67+
// registered will be decoded using the opaqueLeaf type.
68+
//
69+
// Note: if the error type has been migrated from a previous location
70+
// or a different type, ensure that RegisterTypeMigration() was called
71+
// prior to RegisterLeafDecoder().
5272
func RegisterLeafDecoder(typeName TypeKey, decoder LeafDecoder) {
5373
errbase.RegisterLeafDecoder(typeName, decoder)
5474
}
5575

56-
// TypeKey forwards a definition.
76+
// TypeKey identifies an error for the purpose of looking up decoders.
77+
// It is equivalent to the "family name" in ErrorTypeMarker.
5778
type TypeKey = errbase.TypeKey
5879

59-
// GetTypeKey forwards a definition.
80+
// GetTypeKey retrieve the type key for a given error object. This
81+
// is meant for use in combination with the Register functions.
6082
func GetTypeKey(err error) TypeKey { return errbase.GetTypeKey(err) }
6183

62-
// LeafDecoder forwards a definition.
84+
// LeafDecoder is to be provided (via RegisterLeafDecoder above)
85+
// by additional wrapper types not yet known to this library.
86+
// A nil return indicates that decoding was not successful.
6387
type LeafDecoder = errbase.LeafDecoder
6488

65-
// RegisterWrapperDecoder forwards a definition.
89+
// RegisterWrapperDecoder can be used to register new wrapper types to
90+
// the library. Registered wrappers will be decoded using their own
91+
// Go type when an error is decoded. Wrappers that have not been
92+
// registered will be decoded using the opaqueWrapper type.
93+
//
94+
// Note: if the error type has been migrated from a previous location
95+
// or a different type, ensure that RegisterTypeMigration() was called
96+
// prior to RegisterWrapperDecoder().
6697
func RegisterWrapperDecoder(typeName TypeKey, decoder WrapperDecoder) {
6798
errbase.RegisterWrapperDecoder(typeName, decoder)
6899
}
69100

70-
// WrapperDecoder forwards a definition.
101+
// WrapperDecoder is to be provided (via RegisterWrapperDecoder above)
102+
// by additional wrapper types not yet known to this library.
103+
// A nil return indicates that decoding was not successful.
71104
type WrapperDecoder = errbase.WrapperDecoder
72105

73-
// RegisterLeafEncoder forwards a definition.
106+
// RegisterLeafEncoder can be used to register new leaf error types to
107+
// the library. Registered types will be encoded using their own
108+
// Go type when an error is encoded. Wrappers that have not been
109+
// registered will be encoded using the opaqueLeaf type.
110+
//
111+
// Note: if the error type has been migrated from a previous location
112+
// or a different type, ensure that RegisterTypeMigration() was called
113+
// prior to RegisterLeafEncoder().
74114
func RegisterLeafEncoder(typeName TypeKey, encoder LeafEncoder) {
75115
errbase.RegisterLeafEncoder(typeName, encoder)
76116
}
77117

78-
// LeafEncoder forwards a definition.
118+
// LeafEncoder is to be provided (via RegisterLeafEncoder above)
119+
// by additional wrapper types not yet known to this library.
79120
type LeafEncoder = errbase.LeafEncoder
80121

81-
// RegisterWrapperEncoder forwards a definition.
122+
// RegisterWrapperEncoder can be used to register new wrapper types to
123+
// the library. Registered wrappers will be encoded using their own
124+
// Go type when an error is encoded. Wrappers that have not been
125+
// registered will be encoded using the opaqueWrapper type.
126+
//
127+
// Note: if the error type has been migrated from a previous location
128+
// or a different type, ensure that RegisterTypeMigration() was called
129+
// prior to RegisterWrapperEncoder().
82130
func RegisterWrapperEncoder(typeName TypeKey, encoder WrapperEncoder) {
83131
errbase.RegisterWrapperEncoder(typeName, encoder)
84132
}
85133

86-
// WrapperEncoder forwards a definition.
134+
// WrapperEncoder is to be provided (via RegisterWrapperEncoder above)
135+
// by additional wrapper types not yet known to this library.
87136
type WrapperEncoder = errbase.WrapperEncoder
88137

89-
// SetWarningFn forwards a definition.
138+
// SetWarningFn enables configuration of the warning function.
90139
func SetWarningFn(fn func(context.Context, string, ...interface{})) { errbase.SetWarningFn(fn) }
91140

92-
// Formatter is provided for compatibility with xerrors.
93-
// This should probably not be used directly, and
94-
// SafeFormatter preferred instead.
141+
// A Formatter formats error messages.
142+
//
143+
// NB: Consider implementing SafeFormatter instead. This will ensure
144+
// that error displays can distinguish bits that are PII-safe.
95145
type Formatter = errbase.Formatter
96146

97-
// SafeFormatter is like Formatter but supports the separation
98-
// of safe and unsafe strings.
147+
// SafeFormatter is implemented by error leaf or wrapper types that want
148+
// to separate safe and non-safe information when printed out.
149+
//
150+
// When multiple errors are chained (e.g. via errors.Wrap), intermediate
151+
// layers in the error that do not implement SafeError are considered
152+
// “unsafe”
99153
type SafeFormatter = errbase.SafeFormatter
100154

101-
// Printer is provided for compatibility with xerrors.
155+
// A Printer formats error messages.
156+
//
157+
// The most common implementation of Printer is the one provided by package fmt
158+
// during Printf (as of Go 1.13). Localization packages such as golang.org/x/text/message
159+
// typically provide their own implementations.
102160
type Printer = errbase.Printer
103161

104-
// FormatError can be used to implement the fmt.Formatter interface.
162+
// FormatError formats an error according to s and verb.
163+
// This is a helper meant for use when implementing the fmt.Formatter
164+
// interface on custom error objects.
165+
//
166+
// If the error implements errors.Formatter, FormatError calls its
167+
// FormatError method of f with an errors.Printer configured according
168+
// to s and verb, and writes the result to s.
169+
//
170+
// Otherwise, if it is a wrapper, FormatError prints out its error prefix,
171+
// then recurses on its cause.
172+
//
173+
// Otherwise, its Error() text is printed.
105174
func FormatError(err error, s fmt.State, verb rune) { errbase.FormatError(err, s, verb) }
106175

107-
// Formattable can be used to print an error with enhanced detail
108-
// printout when the outer layer of wrapping may not be provided by
109-
// this library.
176+
// Formattable wraps an error into a fmt.Formatter which
177+
// will provide "smart" formatting even if the outer layer
178+
// of the error does not implement the Formatter interface.
110179
func Formattable(err error) fmt.Formatter { return errbase.Formattable(err) }

‎errutil/message.go

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import "github.com/cockroachdb/redact"
1818

1919
// WithMessage annotates err with a new message.
2020
// If err is nil, WithMessage returns nil.
21+
// The message is considered safe for reporting
22+
// and is included in Sentry reports.
2123
func WithMessage(err error, message string) error {
2224
if err == nil {
2325
return nil
@@ -30,6 +32,8 @@ func WithMessage(err error, message string) error {
3032

3133
// WithMessagef annotates err with the format specifier.
3234
// If err is nil, WithMessagef returns nil.
35+
// The message is formatted as per redact.Sprintf,
36+
// to separate safe and unsafe strings for Sentry reporting.
3337
func WithMessagef(err error, format string, args ...interface{}) error {
3438
if err == nil {
3539
return nil

0 commit comments

Comments
 (0)
Please sign in to comment.