Skip to content

refactor: replace direct error comparisons with errors.Is#460

Closed
pixel365 wants to merge 2 commits intosipeed:mainfrom
pixel365:refactor/error-comparisons
Closed

refactor: replace direct error comparisons with errors.Is#460
pixel365 wants to merge 2 commits intosipeed:mainfrom
pixel365:refactor/error-comparisons

Conversation

@pixel365
Copy link
Contributor

📝 Description

Replaced direct error comparisons (err == someErr) with errors.Is.

This ensures correct behavior when errors are wrapped and aligns the codebase with idiomatic Go error handling practices.
No functional changes intended — logic remains the same, comparison mechanism improved.

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

N/A

📚 Technical Context (Skip for Docs)

  • Reference URL: https://pkg.go.dev/errors#Is
  • Reasoning: Direct == comparison does not work reliably with wrapped errors. errors.Is properly unwraps and matches sentinel errors.

🧪 Test Environment

  • Hardware: PC
  • OS: Fedora
  • Model/Provider: N/A
  • Channels: N/A

📸 Evidence (Optional)

Click to view Logs/Screenshots

No behavior changes observed. Existing tests pass.

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

Improves correctness when working with wrapped errors
Copy link

@nikolasdehor nikolasdehor left a comment

Choose a reason for hiding this comment

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

The production code changes are all correct — errors.Is is the idiomatic pattern for wrapped error checking since Go 1.13.

However, I found two reversed argument bugs in the test files:

  1. pkg/providers/error_classifier_test.go: errors.Is(inner, fe.Unwrap()) should be errors.Is(fe.Unwrap(), inner). The first argument is the error chain to search, the second is the target.

  2. pkg/tools/message_test.go: errors.Is(sendErr, result.Err) should be errors.Is(result.Err, sendErr). We're asking "does result.Err match sendErr?", not the reverse.

Both happen to work by coincidence (since the objects are equal), but the semantics are wrong and could mask real bugs if the code is later modified to wrap errors.

Also: the copy -> cp rename in codex_provider.go is unrelated — consider a separate commit or mention in description.

- Fix reversed arguments in errors.Is calls in tests
- Revert cp -> copy rename in codex_provider.go to keep PR focused
@pixel365
Copy link
Contributor Author

@nikolasdehor
Thanks for the detailed review — fixed the errors.Is argument order in both tests.
Also reverted the unrelated cp -> copy rename to keep this PR focused.

Copy link

@nikolasdehor nikolasdehor left a comment

Choose a reason for hiding this comment

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

Correct and idiomatic. All replacements are appropriate:

  • context.Canceled and context.DeadlineExceeded can be wrapped by libraries, so errors.Is is the right approach.
  • http.ErrServerClosed — same reasoning.
  • syscall.EBUSYsyscall.Errno implements error, so errors.Is works here too, though Errno values are typically not wrapped. Still correct and future-proof.

The test updates (fallback_test.go, message_test.go, error_classifier_test.go) are consistent with the production code changes.

LGTM.

@pixel365 pixel365 closed this Mar 4, 2026
@pixel365 pixel365 deleted the refactor/error-comparisons branch March 4, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants