errors icon indicating copy to clipboard operation
errors copied to clipboard

match.As not equivalent errors.As

Open Fryuni opened this issue 4 years ago • 4 comments

package main

import (
	"fmt"

	"emperror.dev/errors"
	"emperror.dev/errors/match"
)

type (
	myErrorKind string
	MyError     struct {
		kind  myErrorKind
		cause error
	}
)

func (e MyError) Error() string {
	return fmt.Sprintf("%s: %+v", e.kind, e.cause)
}

func main() {
	var (
		err1 error = MyError{
			kind:  "my type",
			cause: errors.Sentinel("some error"),
		}
		targetTrueErrors MyError
		targetTrueMatch  MyError
	)

	fromErrorsTrue := errors.As(err1, &targetTrueErrors)
	fromMatchTrue := match.As(&targetTrueMatch).MatchError(err1)

	fmt.Println("Expecting true:")
	fmt.Printf("  From errors: %t\n", fromErrorsTrue)
	fmt.Printf("  From match : %t\n", fromMatchTrue)

	var (
		err2              error = errors.Sentinel("some error")
		targetFalseErrors MyError
		targetFalseMatch  MyError
	)

	fromErrorsFalse := errors.As(err2, &targetFalseErrors)
	fromMatchFalse := match.As(&targetFalseMatch).MatchError(err2)

	fmt.Println("Expecting false:")
	fmt.Printf("  From errors: %t\n", fromErrorsFalse)
	fmt.Printf("  From match : %t\n", fromMatchFalse)
}

// Output:
// Expecting true:
//  From errors: true
//  From match : true
// Expecting false:
//  From errors: false
//  From match : true

I suspect the problem is at this line, but since I'm in a hurry I haven't tried to solve the problem yet

Fryuni avatar Jan 22 '20 15:01 Fryuni

Thanks for opening this issue @Fryuni , sorry I missed it. Looking at it.

sagikazarmark avatar Feb 23 '20 00:02 sagikazarmark

Any news? :)

AlekSi avatar Jul 29 '20 07:07 AlekSi

Sorry, I didn't have time to look at it yet. PRs are always welcome though.

sagikazarmark avatar Jul 29 '20 21:07 sagikazarmark

Took me a long while to get some time to contribute to OSS again. I'll send a PR fixing this in a moment

Fryuni avatar Jan 24 '22 21:01 Fryuni