Skip to content

fix: use %w for error wrapping in WritePacket#1233

Open
Yanhu007 wants to merge 1 commit intogoogle:masterfrom
Yanhu007:fix/write-error-wrapping
Open

fix: use %w for error wrapping in WritePacket#1233
Yanhu007 wants to merge 1 commit intogoogle:masterfrom
Yanhu007:fix/write-error-wrapping

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #1213

Problem

WritePacket in pcapgo/write.go uses %v to format the error from writePacketHeader:

return fmt.Errorf("error writing packet header: %v", err)

This converts the error to a string and loses the original error type, preventing errors.Is and errors.As from detecting underlying system errors (e.g. syscall.ENOSPC for disk full).

Fix

Change %v to %w to properly wrap the error:

return fmt.Errorf("error writing packet header: %w", err)

This preserves the error chain, allowing callers to use errors.Is(err, syscall.ENOSPC) and similar checks.

WritePacket uses %v to format the error from writePacketHeader,
which converts it to a string and loses the original error type.
This prevents errors.Is and errors.As from detecting underlying
system errors like syscall.ENOSPC.

Change %v to %w to properly wrap the error.

Fixes google#1213
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 13, 2026

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.

WritePacket does not properly wrap errors, breaking errors.Is detection (should use %w not %v)

1 participant