fix: use %w in Prefix so errors.Is and errors.As work through prefixed errors#155
Open
amitmishra11 wants to merge 1 commit into
Open
fix: use %w in Prefix so errors.Is and errors.As work through prefixed errors#155amitmishra11 wants to merge 1 commit into
amitmishra11 wants to merge 1 commit into
Conversation
…d errors
Prefix wraps each constituent error with fmt.Errorf using the %s verb,
which formats the original error into a plain string. This means the
original error value is lost, so errors.Is and errors.As cannot find it
when traversing the wrapped chain.
Switching to %w preserves the wrapping relationship. The formatted error
message is identical ("<prefix> <original message>"), so existing callers
that inspect only the string output are unaffected.
Fixes hashicorp#56
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Prefixwraps each constituent error (and non-multierror values) withfmt.Errorf("%s %s", prefix, e). Using the%sverb formats theoriginal error into a plain string, discarding the error value. As a
result,
errors.Isanderrors.Ascannot see through the prefixedwrapper to reach the original error.
Reported in #56.
Reproduction:
Fix
Replace
%swith%win bothfmt.Errorfcalls insidePrefix. The%wverb wraps the original error so the standard library error chaintraversal can reach it. The formatted message is identical
(
"<prefix> <original message>"), so callers that only inspect thestring representation are unaffected.
Test
Added
TestPrefix_ErrorsIswhich asserts thaterrors.Isfinds theoriginal sentinel through both the non-multierror and multierror paths of
Prefix.All existing tests continue to pass.