cockroachdb / cockroachdb/errors
Add errors.AsType helper (Go 1.26+)
- Dominant language
- Go
- Stars
- 2.5k
- Forks
- 74
- PR merge metrics
- No merged PRs in 30d
Description
## Motivation
Go 1.26 introduces `errors.AsType`, a generic helper that makes typed error extraction more concise and safer than the classic `errors.As(err, &target)` pattern without reflect.
`cockroachdb/errors` is widely used as a drop-in replacement for the standard library `errors` for wrapping, stack traces, and utilities. Adding an `AsType`-style API (or mirroring stdlib when building with Go 1.26+) would:
- reduce boilerplate (`var t *T; if errors.As(err, &t) { ... }`)
- improve readability and reduce misuse
- align the package with modern Go idioms (generic helpers)
- help projects keep a single “errors surface” without mixing stdlib + `cockroachdb/errors`
## Request
Add `errors.AsType` to this package with semantics matching Go 1.26 `errors.AsType`.
- On Go 1.26+: delegate to stdlib `errors.AsType`.
- Use `go:build` constraints to keep compatibility clean.
```go
//go:build go1.26
package errors
import (
sterrors "errors"
)
// AsType delegates to the standard library implementation on Go 1.26+.
func AsType[T any](err error) (T, bool) {
return sterrors.AsType[T](err)
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading the package's existing public error helpers and its Go-version or build-tag organization; the issue does not name specific files or tests. Verify the intended Go 1.26 semantics and compatibility behavior, then run the repository's existing test suite to confirm the helper works on supported versions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100