Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat errkit #416

Merged
merged 14 commits into from
May 22, 2024
Merged

Feat errkit #416

merged 14 commits into from
May 22, 2024

Conversation

tarunKoyalwar
Copy link
Member

@tarunKoyalwar tarunKoyalwar commented May 15, 2024

Proposed Changes

  • errkit is new go package that aims to be replacement of existing err packages, it is built upon from experiences and issues of errorutil and is drop in replacement for many known popular packages errors github.com/pkg/errors go.uber.go/multierr and fits well in existing error package style/ecosystem

utils/errkit/README.md

Lines 1 to 58 in 389764f

# errkit
why errkit when we already have errorutil ?
----
Introduced a year ago, `errorutil` aimed to capture error stacks for identifying deeply nested errors. However, its approach deviates from Go's error handling paradigm. In Go, libraries like "errors", "pkg/errors", and "uber.go/multierr" avoid using the `.Error()` method directly. Instead, they wrap errors with helper structs that implement specific interfaces, facilitating error chain traversal and the use of helper functions like `.Cause() error` or `.Unwrap() error` or `errors.Is()`. Contrarily, `errorutil` marshals errors to strings, which is incompatible with Go's error handling paradigm. Over time, the use of `errorutil` has become cumbersome due to its inability to replace any error package seamlessly and its lack of support for idiomatic error propagation or traversal in Go.
`errkit` is a new error library that addresses the shortcomings of `errorutil`. It offers the following features:
- Seamless replacement for existing error packages, requiring no syntax changes or refactoring:
- `errors` package
- `pkg/errors` package (now deprecated)
- `uber/multierr` package
- `errkit` is compatible with all known Go error handling implementations. It can parse errors from any library and works with existing error handling libraries and helper functions like `Is()`, `As()`, `Cause()`, and more.
- `errkit` is Go idiomatic and adheres to the Go error handling paradigm.
- `errkit` supports attributes for structured error information or logging using `slog.Attr` (optional).
- `errkit` implements and categorizes errors into different kinds, as detailed below.
- `ErrKindNetworkTemporary`
- `ErrKindNetworkPermanent`
- `ErrKindDeadline`
- Custom kinds via `ErrKind` interface
- `errkit` provides helper functions for structured error logging using `SlogAttrs` and `SlogAttrGroup`.
- `errkit` offers helper functions to implement public or user-facing errors by using error kinds interface.
**Attributes Support**
`errkit` supports optional error wrapping with attributes `slog.Attr` for structured error logging, providing a more organized approach to error logging than string wrapping.
```go
// normal way of error propogating through nested stack
err := errkit.New("i/o timeout")
// xyz.go
err := errkit.Wrap(err,"failed to connect %s",addr)
// abc.go
err := errkit.Wrap(err,"error occured when downloading %s",xyz)
```
with attributes support you can do following
```go
// normal way of error propogating through nested stack
err := errkit.New("i/o timeout")
// xyz.go
err = errkit.WithAttr(err,slog.Any("resource",domain))
// abc.go
err = errkit.WithAttr(err,slog.Any("action","download"))
```
## Note
To keep errors concise and avoid unnecessary allocations, message wrapping and attributes count have a max depth set to 3. Adding more will not panic but will be simply ignored. This is configurable using the MAX_ERR_DEPTH env variable (default 3).

@tarunKoyalwar tarunKoyalwar self-assigned this May 15, 2024
@tarunKoyalwar tarunKoyalwar marked this pull request as draft May 19, 2024 19:00
@tarunKoyalwar tarunKoyalwar marked this pull request as ready for review May 22, 2024 17:48
@ehsandeep ehsandeep requested a review from Mzack9999 May 22, 2024 18:31
@tarunKoyalwar
Copy link
Member Author

@Mzack9999 , merging it now because to test this in other projects of pdcp , a new release is necessary since there is a conflicting version upgrade between shuffledns(uses latest version) vs current one( is based out on previous version not latest)

if you find any bugs/issues we can fix that in follow up PR 😄

@tarunKoyalwar tarunKoyalwar merged commit 10ef59b into main May 22, 2024
7 checks passed
@tarunKoyalwar tarunKoyalwar deleted the feat-errkit branch May 22, 2024 20:42
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.

None yet

1 participant