Skip to content

fix: wrap the Parse error in UUID.Scan with %w - #229

Open
januththedev wants to merge 2 commits into
google:masterfrom
januththedev:fix/scan-error-wrap
Open

januththedev wants to merge 2 commits into
google:masterfrom
januththedev:fix/scan-error-wrap

Conversation

@januththedev

Copy link
Copy Markdown

Summary

UUID.Scan flattened the Parse error with %v, discarding its identity so errors.Is/errors.As could not classify it.

u, err := Parse(src)
if err != nil {
    return fmt.Errorf("Scan: %v", err)   // loses the wrapped error
}

PR #166 (shipped in v1.6.0) added ErrInvalidLength, ErrInvalidUUIDFormat, ErrInvalidURNPrefix, ErrInvalidBracketedFormat and IsInvalidLengthError() specifically so callers could use errors.Is, and it wrapped Parse, ParseBytes and Validate with %w. UUID.Scan — the primary entry point for reading UUIDs out of a SQL driver — was missed, and sql.go:29 is the only fmt.Errorf in the package that flattens a real error. The []byte branch recurses into the string branch, so it is affected too, as is NullUUID.Scan which delegates.

Note the error text was already correct. Only the error type was lost, which is why this has gone unnoticed.

Reproduction

_, err := uuid.Parse("12345")
_, scanErr := (&uuid.UUID{}).Scan("12345")
fmt.Println(errors.Is(scanErr, uuid.ErrInvalidLength))   // false, should be true
Parse err=invalid UUID length: 5
Scan  err=Scan: invalid UUID length: 5
IsInvalidLength(ScanErr)=false

The change

return fmt.Errorf("Scan: %w", err)

Tests

TestScanErrorWrapping is table-driven with six subtests covering string and []byte inputs for all three error types. Each asserts both that errors.Is now matches the sentinel and that the message text is byte-identical to before, which is what demonstrates the change is non-breaking.

  • Before: 6/6 subtests fail. After: 6/6 pass.
  • go test -count=1 ./... → 0 failures, 173 subtests passing.
  • go vet ./... clean.

The only formatting note is that gofmt -l flags most files in this checkout, which is pre-existing CRLF line endings from Windows; a pristine clone produces the identical list.

@januththedev
januththedev requested a review from a team as a code owner September 27, 2026 02:12
@google-cla

google-cla Bot commented Sep 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant