To be clear: You seem to assume that `errors.Is` recursively unwraps both errors passed to it and checks for equality along any of their pairings (or maybe in the innermost wrapped error). But that's not the case. errors.Is checks if the first error wraps the second. One error wrapping another is not an equivalence relationship. eGot wrapping ErrInvalidArgument does not mean ErrInvalidArgument wraps eGot. eExpected also wrapping ErrInvalidArgument also does not mean that eGot wraps eExpected (or vice versa). One error wrapping another is an asymmetric relationship between the two - can you get to the second by recursively unwrapping the first.
On Sun, May 1, 2022 at 8:46 PM Axel Wagner <axel.wagner...@googlemail.com> wrote: > > > On Sun, May 1, 2022 at 6:12 PM Tong Sun <suntong...@gmail.com> wrote: > >> >> >> On Sunday, May 1, 2022 at 11:17:35 AM UTC-4 axel.wa...@googlemail.com >> wrote: >> >>> In the first case, you ask if the respective errors wrap e1. The answer >>> is yes - both wrap e1. >>> In the second case, you ask if one error wraps the other. The answer is >>> no - they both wrap ErrInvalidArgument, but they don't wrap each other. >>> >> >> Are you talking about the following two checks? >> >> if errors.Is(e2, e1) >> > > This checks if e2 wraps e1. Which it does, as it was created via > return fmt.Errorf("I'm adding further more context: %w", e1) > > >> if errors.Is(eGot, eExpected) >> > > This checks if eGot wraps eExpected. Which it doesn't. They are > constructed as > eExpected := fmt.Errorf("grpc: InvalidArgument, cannot deposit %v: %w", > -1.11, ErrInvalidArgument) > eGot := fmt.Errorf("cannot deposit %v: %w", -1.11, ErrInvalidArgument) > Clearly, eGot does not wrap eExpected. Both of them wrap > ErrInvalidArgument. But that is not the same as wrapping each other. > > If you want to check that they are both wrapping ErrInvalidArgument, use > errors.Is(eGot, ErrInvalidArgument) > errors.Is(eExpected, ErrInvalidArgument) > > If you want to make eGot wrap eExpected, use > eGot := fmt.Errorf("cannot deposit %v: %w", -1.11, eExpected) > > >> I see both are checking if the respective errors are wrapping (from) the >> same error. >> >> Why one works and one doesn't? >> >> errors.Is doesn't check if both errors passed wrap the same error - they >>> check if the first argument wraps the other. >>> >>> On Sun, May 1, 2022 at 4:56 PM Tong Sun <sunto...@gmail.com> wrote: >>> >>>> Please take a look at >>>> https://go.dev/play/p/Dl_IGD46bPe >>>> >>>> I have two error wrappings there, one works and one doesn't (I'm >>>> expecting both to be the "Same"). Why one works and one doesn't? >>>> >>>> thanks >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "golang-nuts" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to golang-nuts...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/3a1c9ad1-8d7c-481a-bfbb-4d4c94cc8f72n%40googlegroups.com >>>> <https://groups.google.com/d/msgid/golang-nuts/3a1c9ad1-8d7c-481a-bfbb-4d4c94cc8f72n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/47ceb9a6-8b4f-441b-b694-438ac8764c01n%40googlegroups.com >> <https://groups.google.com/d/msgid/golang-nuts/47ceb9a6-8b4f-441b-b694-438ac8764c01n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE%2BgegP5dGSxKr_Oe%2BJgAJ55WSMmGMSd28KByOyVSPEKA%40mail.gmail.com.