@@ -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).
2530func 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.
2834func 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 .
3137type EncodedError = errbase.EncodedError
3238
33- // EncodeError forwards a definition .
39+ // EncodeError encodes an error .
3440func EncodeError (ctx context.Context , err error ) EncodedError { return errbase .EncodeError (ctx , err ) }
3541
36- // DecodeError forwards a definition .
42+ // DecodeError decodes an error .
3743func 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.
4048type 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.
4353func 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.
4658func 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.
4962type 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().
5272func 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.
5778type 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.
6082func 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.
6387type 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().
6697func 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.
71104type 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().
74114func 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.
79120type 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().
82130func 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.
87136type WrapperEncoder = errbase.WrapperEncoder
88137
89- // SetWarningFn forwards a definition .
138+ // SetWarningFn enables configuration of the warning function .
90139func 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.
95145type 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”
99153type 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.
102160type 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.
105174func 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 .
110179func Formattable (err error ) fmt.Formatter { return errbase .Formattable (err ) }
0 commit comments