Skip to content

refactor: use errors.As/Is for idiomatic error checks - #363

Open
tenthirtyam wants to merge 2 commits into
hashicorp:mainfrom
tenthirtyam:refactor/idiomatic-error-checks
Open

tenthirtyam wants to merge 2 commits into
hashicorp:mainfrom
tenthirtyam:refactor/idiomatic-error-checks

Conversation

@tenthirtyam

Copy link
Copy Markdown
Contributor

Description

Replace direct type assertions and equality checks with errors.As and errors.Is to properly handle wrapped errors.

Rollback Plan

Revert commit.

Changes to Security Controls

None

@tenthirtyam
tenthirtyam requested a review from a team as a code owner September 9, 2026 14:57
@tenthirtyam
tenthirtyam force-pushed the refactor/idiomatic-error-checks branch from c7a38b8 to 5b4a90b Compare September 9, 2026 19:50
Replace direct type assertions and equality checks with `errors.As` and `errors.Is` to properly handle wrapped errors.

Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Typed-nil *MultiError values are incorrectly retained instead of treated as empty or ignored, producing spurious <nil> entries and inflated validation error counts.

2 open issues
What changed in this PR

This PR updates error checks across configuration decoding, SSH communication, filesystem syncing, and error aggregation to recognize wrapped errors using errors.As and errors.Is.

Changes:

  • Replace direct error type assertions with errors.As.
  • Replace sentinel error comparisons with errors.Is, including the SSH handshake timeout test.
  • Refactor MultiErrorAppend to identify nested multi-errors.
File Description
template/​config/​decode.go Recognize wrapped cty.PathError values during decoding.
sdk-internals/​communicator/​ssh/​communicator.go Recognize wrapped SSH exit errors.
sdk-internals/​communicator/​ssh/​communicator_test.go Use errors.Is for handshake timeout checks.
packer/​multi_error.go Use errors.As when accumulating multi-errors.
cmd/​packer-sdc/​internal/​fs/​sync.go Recognize wrapped link and Windows privilege errors.

Review effort: Balanced

Comment thread packer/multi_error.go Resolved
Comment thread packer/multi_error.go Resolved
A nil MultiError was counted as a real error, so the first validation failure showed up twice and one entry was "<nil>". Treat that nil as empty again.

Signed-off-by: Ryan Johnson <ryan@tenththirtyam.org>
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.

2 participants