Error wrapping not always work??

118 views
Skip to first unread message

Tong Sun

unread,
May 1, 2022, 10:55:46 AM5/1/22
to golang-nuts
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

Axel Wagner

unread,
May 1, 2022, 11:17:35 AM5/1/22
to Tong Sun, golang-nuts
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.
errors.Is doesn't check if both errors passed wrap the same error - they check if the first argument wraps the other.

--
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.

Tong Sun

unread,
May 1, 2022, 12:12:41 PM5/1/22
to golang-nuts
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)
if errors.Is(eGot, eExpected)

I see both are checking if the respective errors are wrapping (from) the same error.

Axel Wagner

unread,
May 1, 2022, 2:46:57 PM5/1/22
to Tong Sun, golang-nuts
On Sun, May 1, 2022 at 6:12 PM Tong Sun <sunto...@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.

--
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.

Axel Wagner

unread,
May 1, 2022, 2:53:29 PM5/1/22
to Tong Sun, golang-nuts
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.

Tong Sun

unread,
May 1, 2022, 3:40:20 PM5/1/22
to golang-nuts
Got it. Thanks a lot sir.
Reply all
Reply to author
Forward
0 new messages