Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

chore: Replace errors.As with generic As and AsInterface#63047

Merged
varungandhi-src merged 3 commits into
mainfrom
vg/better-As
Jun 4, 2024
Merged

chore: Replace errors.As with generic As and AsInterface#63047
varungandhi-src merged 3 commits into
mainfrom
vg/better-As

Conversation

@varungandhi-src

Copy link
Copy Markdown
Contributor

Splits the signature of errors.As into two more specialized functions
which catch more errors at compile-time using generics.

Test plan

Covered by existing tests, added some new tests.

Changelog

@cla-bot cla-bot Bot added the cla-signed label Jun 3, 2024
@github-actions github-actions Bot added team/graph Graph Team (previously Code Intel/Language Tools/Language Platform) team/product-platform labels Jun 3, 2024
Comment on lines -123 to -126
require.Panics(t, func() {
var check payloadLessPtrError
require.True(t, As(err, &check))
})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These panics become type errors, so we can just remove them instead of checking for panics.

@camdencheek camdencheek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very nice, thank you!

cc @bobheadxi just because I think you probably have the most context around lib/errors

Comment thread lib/errors/cockroach.go Outdated
Comment thread lib/errors/multi_error.go
import (
"fmt"

"github.com/cockroachdb/errors" //nolint:depguard // needed for implementation of multiError.As

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need the upstream As, instead of your new As?

@camdencheek camdencheek Jun 3, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can constrain our usage of errors.As with the new interface, but we shouldn't change the implementation of interface{ As() } to be specific to this library because multierrors created by this library can be passed to code that doesn't use this library. Otherwise, 3rd-party code that calls, e.g., var netErr net.DNSError ; errors.As(err, &netErr) on an error from this library will no longer work correctly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we shouldn't change the implementation of interface{ As() } to be specific to this library

Ah, this clicks for me 😁 Could we add a brief docstring to this effect where we use As?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could probably also use the stdlib errors.As() here. Cockroach shouldn't be adding any special behavior to that one

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've tried to make this change semantically a no-op as much as possible. It's potentially a bit riskier to move between libraries (ideally they should have the same semantics but...), we can make that change in a follow-up PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could we add a brief docstring to this effect where we use As?

Added here: https://github.com/sourcegraph/sourcegraph/pull/63047/commits/bbf7a9f38d86a061b97eb95f4cd5de59f706fe59

@varungandhi-src varungandhi-src enabled auto-merge (squash) June 4, 2024 01:41
@varungandhi-src varungandhi-src merged commit d7f3e54 into main Jun 4, 2024
@varungandhi-src varungandhi-src deleted the vg/better-As branch June 4, 2024 01:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed team/graph Graph Team (previously Code Intel/Language Tools/Language Platform) team/product-platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants