MCPcopy Index your code
hub / github.com/cockroachdb/errors / encodeWrapper

Function encodeWrapper

errbase/encode.go:131–182  ·  view source on GitHub ↗

encodeWrapper encodes an error wrapper.

(ctx context.Context, err, cause error)

Source from the content-addressed store, hash-verified

129
130// encodeWrapper encodes an error wrapper.
131func encodeWrapper(ctx context.Context, err, cause error) EncodedError {
132 var msg string
133 var details errorspb.EncodedErrorDetails
134 messageType := Prefix
135
136 if e, ok := err.(*opaqueWrapper); ok {
137 // We delegate all knowledge of the error string
138 // to the original encoder and do not try to re-engineer
139 // the prefix out of the error. This helps maintain
140 // backward compatibility with earlier versions of the
141 // encoder which don't have any understanding of
142 // error string ownership by the wrapper.
143 msg = e.prefix
144 details = e.details
145 messageType = e.messageType
146 } else {
147 details.OriginalTypeName, details.ErrorTypeMark.FamilyName, details.ErrorTypeMark.Extension = getTypeDetails(err, false /*onlyFamily*/)
148
149 var payload proto.Message
150
151 // If we have a manually registered encoder, use that.
152 typeKey := TypeKey(details.ErrorTypeMark.FamilyName)
153 if enc, ok := encoders[typeKey]; ok {
154 msg, details.ReportablePayload, payload, messageType = enc(ctx, err)
155 } else {
156 // No encoder.
157 // In that case, we'll try to compute a message prefix
158 // manually.
159 msg, messageType = extractPrefix(err, cause)
160
161 // If there are known safe details, use them.
162 if s, ok := err.(SafeDetailer); ok {
163 details.ReportablePayload = s.SafeDetails()
164 }
165
166 // That's all we can get.
167 }
168 // If there is a detail payload, encode it.
169 details.FullDetails = encodeAsAny(ctx, err, payload)
170 }
171
172 return EncodedError{
173 Error: &errorspb.EncodedError_Wrapper{
174 Wrapper: &errorspb.EncodedWrapper{
175 Cause: EncodeError(ctx, cause),
176 Message: msg,
177 Details: details,
178 MessageType: errorspb.MessageType(messageType),
179 },
180 },
181 }
182}
183
184// extractPrefix extracts the prefix from a wrapper's error message.
185// For example,

Callers 1

EncodeErrorFunction · 0.85

Calls 7

MessageTypeTypeAlias · 0.92
getTypeDetailsFunction · 0.85
TypeKeyTypeAlias · 0.85
extractPrefixFunction · 0.85
encodeAsAnyFunction · 0.85
EncodeErrorFunction · 0.70
SafeDetailsMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…