@@ -21,90 +21,159 @@ import (
21
21
"github.com/cockroachdb/errors/errbase"
22
22
)
23
23
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).
25
30
func UnwrapOnce (err error ) error { return errbase .UnwrapOnce (err ) }
26
31
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.
28
34
func UnwrapAll (err error ) error { return errbase .UnwrapAll (err ) }
29
35
30
- // EncodedError forwards a definition .
36
+ // EncodedError is the type of an encoded (and protobuf-encodable) error .
31
37
type EncodedError = errbase.EncodedError
32
38
33
- // EncodeError forwards a definition .
39
+ // EncodeError encodes an error .
34
40
func EncodeError (ctx context.Context , err error ) EncodedError { return errbase .EncodeError (ctx , err ) }
35
41
36
- // DecodeError forwards a definition .
42
+ // DecodeError decodes an error .
37
43
func DecodeError (ctx context.Context , enc EncodedError ) error { return errbase .DecodeError (ctx , enc ) }
38
44
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.
40
48
type SafeDetailer = errbase.SafeDetailer
41
49
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.
43
53
func GetAllSafeDetails (err error ) []SafeDetailPayload { return errbase .GetAllSafeDetails (err ) }
44
54
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.
46
58
func GetSafeDetails (err error ) (payload SafeDetailPayload ) { return errbase .GetSafeDetails (err ) }
47
59
48
- // SafeDetailPayload forwards a definition.
60
+ // SafeDetailPayload captures the safe strings for one
61
+ // level of wrapping.
49
62
type SafeDetailPayload = errbase.SafeDetailPayload
50
63
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().
52
72
func RegisterLeafDecoder (typeName TypeKey , decoder LeafDecoder ) {
53
73
errbase .RegisterLeafDecoder (typeName , decoder )
54
74
}
55
75
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.
57
78
type TypeKey = errbase.TypeKey
58
79
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.
60
82
func GetTypeKey (err error ) TypeKey { return errbase .GetTypeKey (err ) }
61
83
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.
63
87
type LeafDecoder = errbase.LeafDecoder
64
88
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().
66
97
func RegisterWrapperDecoder (typeName TypeKey , decoder WrapperDecoder ) {
67
98
errbase .RegisterWrapperDecoder (typeName , decoder )
68
99
}
69
100
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.
71
104
type WrapperDecoder = errbase.WrapperDecoder
72
105
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().
74
114
func RegisterLeafEncoder (typeName TypeKey , encoder LeafEncoder ) {
75
115
errbase .RegisterLeafEncoder (typeName , encoder )
76
116
}
77
117
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.
79
120
type LeafEncoder = errbase.LeafEncoder
80
121
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().
82
130
func RegisterWrapperEncoder (typeName TypeKey , encoder WrapperEncoder ) {
83
131
errbase .RegisterWrapperEncoder (typeName , encoder )
84
132
}
85
133
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.
87
136
type WrapperEncoder = errbase.WrapperEncoder
88
137
89
- // SetWarningFn forwards a definition .
138
+ // SetWarningFn enables configuration of the warning function .
90
139
func SetWarningFn (fn func (context.Context , string , ... interface {})) { errbase .SetWarningFn (fn ) }
91
140
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.
95
145
type Formatter = errbase.Formatter
96
146
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”
99
153
type SafeFormatter = errbase.SafeFormatter
100
154
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.
102
160
type Printer = errbase.Printer
103
161
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.
105
174
func FormatError (err error , s fmt.State , verb rune ) { errbase .FormatError (err , s , verb ) }
106
175
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 .
110
179
func Formattable (err error ) fmt.Formatter { return errbase .Formattable (err ) }
0 commit comments