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

errorsbp: Fix a corner case in BatchSize #592

Merged
merged 1 commit into from
Jan 9, 2023
Merged

Conversation

fishy
Copy link
Member

@fishy fishy commented Jan 6, 2023

I missed a corner case in 1edc07c, that if a batched error was single wrapped again BatchSize would not recognize it as batched error. Fix the issue and updated the test case to cover it.

@fishy fishy requested a review from a team as a code owner January 6, 2023 17:45
@fishy fishy requested review from kylelemons, konradreiche and mterwill and removed request for a team January 6, 2023 17:45
@@ -215,7 +214,8 @@ func BatchSize(err error) int {
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

(above) If err implements is no longer quite true, since it works if any error in the tree implements Unwrap().

It's also probably worth noting that, with the support we're adding, BatchSize() != len(GetErrors()). Do we want that invariant to be true?

Does BatchSize need to be an xFS traversal using Unwrap() error / Unwrap() []error to actually count the number of errors? now that we're using errors.As, it seems like it might...

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll update the doc.

As for BatchSize() != len(GetErrors()), yes I'm aware and that's an intentional decision from the last PR. The rational is that even though we try to flatten it while adding to the batch, we only want to flatten it when we can be sure that we won't lose important context when doing so, which is not a guarantee if we try to flatten everything that implements Unwrap() []error, here is an example:

func foo(...) error {
  ...
  return fmt.Errorf("some very important context info: %w, %w", err1, err2)
}

func bar(...) error {
  var batch errorsbp.Batch
  ...
  batch.Add(foo(...)) // if we try to flatten it here, we have no easy way to preserve the context info from foo
  ...
  return batch.Compile()
}

and also that's only a problem if the code somehow mixes errorsbp.Batch with errors.Join/fmt.Errorf batches. the hope is that

  1. people should stop using errorsbp.Batch if they already upgraded to 1.20 and only use errors.Join/fmt.Errorf instead
  2. we'll deprecate errorsbp.Batch as soon as we drop support for 1.19.

As to your last point, I don't think that's necessary. errors.As(err, &unwrapper) will do Unwrap() error for us until it finds the first Unwrap() []error, and those Unwrap() error layers it skipped will not really increase the number of wrapped errors in the tree.

Copy link
Member Author

Choose a reason for hiding this comment

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

do you want me to update the doc comment to also call out/explain the BatchSize() != len(GetErrors()) case?

Copy link
Contributor

@kylelemons kylelemons Jan 6, 2023

Choose a reason for hiding this comment

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

yeah, I think it's worth documenting explicitly BatchSize() != len(GetErrors()) to reduce the chance that someone tries that on accident thinking it'll work.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@fishy fishy requested a review from kylelemons January 6, 2023 19:39
I missed a corner case in 1edc07c, that if a batched error was single
wrapped again BatchSize would not recognize it as batched error. Fix the
issue and updated the test case to cover it.
@fishy fishy merged commit 742260a into reddit:master Jan 9, 2023
@fishy fishy deleted the errorsbp-fix branch January 9, 2023 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants