-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, support for the `wrapError` type generated by `fmt.Errorf("%w xxxx")` was missing an understanding of the fact that the format string wasn't limited to just prefixes. Hence the `opaqueWrapper` struct assumed that all wrappers had prefix strings that would precede their causal chains. This change adds an enum to the `EncodedWrapper` which tracks whether the wrapper's error string is a prefix, or the full error. In the case of `fmt.Errorf` the `wrapError` struct created within the stdlib contains a fully interpolated string as its message, not the format string, or a prefix. This means that when we encode it into a `EncodedWrapper` for transfer over the network, we need to remember to just print the wrapper's string when `.Error()` is called on the decoded object on the other end. Paired with this change the `opaqueWrapper` that is generated on the decode side now contains this field as well which is referenced when calling `.Error()` on the wrapper, ensuring we don't re-concatenate the causes. This field is also referenced when formatting the error in `state.formatSingleLineOutput` where we use it to simply output the final entry's message, instead of concatenating all the entries together. This change is backwards compatible since older versions will omit the new field in the proto, defaulting it to `PREFIX` in newer versions, which will preserve existing behavior when printing out errors. New code will set the field when necessary and interpret it appropriately. If an error encoded with a new version is decoded by an older version, the information about the error string will be discarded, which may lead to repeated portions of the error message when printed out, if the wrapper contains a copy of the message of its cause.
- Loading branch information
1 parent
1456059
commit 774f530
Showing
29 changed files
with
7,412 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package errbase_test | ||
|
||
import ( | ||
"context" | ||
goErr "errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/errors/errbase" | ||
"github.com/cockroachdb/errors/errorspb" | ||
"github.com/cockroachdb/errors/testutils" | ||
) | ||
|
||
func genEncoded(mt errorspb.MessageType) errorspb.EncodedError { | ||
return errorspb.EncodedError{ | ||
Error: &errorspb.EncodedError_Wrapper{ | ||
Wrapper: &errorspb.EncodedWrapper{ | ||
Cause: errorspb.EncodedError{ | ||
Error: &errorspb.EncodedError_Leaf{ | ||
Leaf: &errorspb.EncodedErrorLeaf{ | ||
Message: "leaf-error-msg", | ||
}, | ||
}, | ||
}, | ||
Message: "wrapper-error-msg: leaf-error-msg: extra info", | ||
Details: errorspb.EncodedErrorDetails{}, | ||
MessageType: mt, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func TestDecodeOldVersion(t *testing.T) { | ||
tt := testutils.T{T: t} | ||
|
||
errOldEncoded := genEncoded(errorspb.MessageType_PREFIX) | ||
errOldDecoded := errbase.DecodeError(context.Background(), errOldEncoded) | ||
// Ensure that we will continue to just concat leaf after wrapper | ||
// with older errors for backward compatibility. | ||
tt.CheckEqual(errOldDecoded.Error(), "wrapper-error-msg: leaf-error-msg: extra info: leaf-error-msg") | ||
|
||
// Check to ensure that when flag is present, we interpret things correctly. | ||
errNewEncoded := genEncoded(errorspb.MessageType_FULL_MESSAGE) | ||
errNewDecoded := errbase.DecodeError(context.Background(), errNewEncoded) | ||
tt.CheckEqual(errNewDecoded.Error(), "wrapper-error-msg: leaf-error-msg: extra info") | ||
} | ||
|
||
func TestEncodeDecodeNewVersion(t *testing.T) { | ||
tt := testutils.T{T: t} | ||
errNewEncoded := errbase.EncodeError( | ||
context.Background(), | ||
fmt.Errorf( | ||
"wrapper-error-msg: %w: extra info", | ||
goErr.New("leaf-error-msg"), | ||
), | ||
) | ||
|
||
errNew := errorspb.EncodedError{ | ||
Error: &errorspb.EncodedError_Wrapper{ | ||
Wrapper: &errorspb.EncodedWrapper{ | ||
Cause: errorspb.EncodedError{ | ||
Error: &errorspb.EncodedError_Leaf{ | ||
Leaf: &errorspb.EncodedErrorLeaf{ | ||
Message: "leaf-error-msg", | ||
Details: errorspb.EncodedErrorDetails{ | ||
OriginalTypeName: "errors/*errors.errorString", | ||
ErrorTypeMark: errorspb.ErrorTypeMark{FamilyName: "errors/*errors.errorString", Extension: ""}, | ||
ReportablePayload: nil, | ||
FullDetails: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Message: "wrapper-error-msg: leaf-error-msg: extra info", | ||
Details: errorspb.EncodedErrorDetails{ | ||
OriginalTypeName: "fmt/*fmt.wrapError", | ||
ErrorTypeMark: errorspb.ErrorTypeMark{FamilyName: "fmt/*fmt.wrapError", Extension: ""}, | ||
ReportablePayload: nil, | ||
FullDetails: nil, | ||
}, | ||
MessageType: errorspb.MessageType_FULL_MESSAGE, | ||
}, | ||
}, | ||
} | ||
|
||
tt.CheckDeepEqual(errNewEncoded, errNew) | ||
newErr := errbase.DecodeError(context.Background(), errNew) | ||
|
||
// New version correctly decodes error | ||
tt.CheckEqual(newErr.Error(), "wrapper-error-msg: leaf-error-msg: extra info") | ||
} |
Oops, something went wrong.