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

api/types/filters: fix errors not being matched by errors.Is() #46106

Merged
merged 1 commit into from
Jul 29, 2023

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Jul 28, 2023

I found that the errors returned weren't matched with errors.Is() when wrapped.

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah

This comment was marked as outdated.

I found that the errors returned weren't matched with `errors.Is()` when
wrapped.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah marked this pull request as ready for review July 28, 2023 21:07
@thaJeztah
Copy link
Member Author

@vvoland @cpuguy83 ptal 🤗

@cpuguy83
Copy link
Member

Oh I hate this semantic. Too easy to mess it up.
For buildkit I have a pending change that just implements Is and checks both for *<type> and **<type>

@thaJeztah
Copy link
Member Author

Too easy to mess it up.

I KNOW! I spend quite some time scratching my head "WHY DOESN'T THIS WORK?"

... and all that was caused because I wanted to update containerd vendoring to 1.6.22, which also updated gotest.tools .. and then I hit;

So... massive amounts of yak-shaving for me today 😂

@thaJeztah
Copy link
Member Author

This was the other fun one; adding/removing the Value []string slice made it work or break;

https://go.dev/play/p/_3axGxDC8S8

package main

import (
	"errors"
	"fmt"
	"testing"
)

type customError struct {
	Name string
}

func (e customError) Error() string {
	return fmt.Sprintf("invalid name: %s", e.Name)
}

func (e customError) InvalidParameter() {}

type customError2 struct {
	Name  string
	Value []string
}

func (e customError2) Error() string {
	return fmt.Sprintf("invalid name: %s (%s)", e.Name, e.Value)
}

func (e customError2) InvalidParameter() {}

type customError3 struct {
	Name  string
	Value []string
}

func (e *customError3) Error() string {
	return fmt.Sprintf("invalid name: %s (%s)", e.Name, e.Value)
}

func (e *customError3) InvalidParameter() {}

func TestCustomErrorIs(t *testing.T) {
	err1 := customError{Name: "hello"}
	wrappedErr1 := fmt.Errorf("something went wrong: %w", err1)
	if !errors.Is(wrappedErr1, err1) {
		t.Errorf("customError: %v != %v", wrappedErr1, err1)
	}

	err2 := customError2{Name: "hello"}
	wrappedErr2 := fmt.Errorf("something went wrong: %w", err2)
	if !errors.Is(wrappedErr2, err2) {
		t.Errorf("customError2: %v != %v", wrappedErr2, err2)
	}

	err3 := &customError2{Name: "hello"}
	wrappedErr3 := fmt.Errorf("something went wrong: %w", err3)
	if !errors.Is(wrappedErr3, err3) {
		t.Errorf("*customError2: %v != %v", wrappedErr3, err3)
	}

	err4 := &customError3{Name: "hello"}
	wrappedErr4 := fmt.Errorf("something went wrong: %w", err4)
	if !errors.Is(wrappedErr4, err4) {
		t.Errorf("customError3: %v != %v", wrappedErr4, err4)
	}
}

@thaJeztah thaJeztah merged commit 0772082 into moby:master Jul 29, 2023
103 checks passed
@thaJeztah thaJeztah deleted the fix_filter_errors branch July 29, 2023 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants