diff --git a/changelog/@unreleased/pr-643.v2.yml b/changelog/@unreleased/pr-643.v2.yml new file mode 100644 index 000000000..005f13ba4 --- /dev/null +++ b/changelog/@unreleased/pr-643.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Generated errors IsError functions match by name, not go type assertion + links: + - https://github.com/palantir/conjure-go/pull/643 diff --git a/conjure/errorwriter.go b/conjure/errorwriter.go index ad2b26243..13c8e4c9a 100644 --- a/conjure/errorwriter.go +++ b/conjure/errorwriter.go @@ -500,19 +500,17 @@ func astErrorInitFunc(file *jen.Group, defs []*types.ErrorDefinition, errorRegis // astIsErrorTypeFunc generates a helper func that checks whether an error is of the error type: // // func IsMyNotFound(err error) bool { -// if err == nil { -// return false -// } -// _, ok := errors.GetConjureError(err).(*MyNotFound) -// return ok +// cerr := errors.GetConjureError(err) +// return cerr != nil && cerr.Name() == "MyNamespace:MyNotFound" // } func astIsErrorTypeFunc(file *jen.Group, def *types.ErrorDefinition) { name := "Is" + def.Name file.Commentf("%s returns true if err is an instance of %s.", name, def.Name) file.Func().Id(name).Params(jen.Err().Error()).Params(jen.Bool()).Block( - jen.If(jen.Err().Op("==").Nil()).Block(jen.Return(jen.False())), - jen.List(jen.Id("_"), jen.Id("ok")).Op(":=").Add(snip.CGRErrorsGetConjureError()).Call(jen.Err()).Assert(jen.Op("*").Id(def.Name)), - jen.Return(jen.Id("ok")), + jen.Id("cErr").Op(":=").Add(snip.CGRErrorsGetConjureError()).Call(jen.Err()), + jen.Return(jen.Id("cErr").Op("!=").Nil(). + Op("&&"). + Id("cErr").Dot("Name").Call().Op("==").Lit(fmt.Sprintf("%s:%s", def.ErrorNamespace, def.Name))), ) } diff --git a/cycles/testdata/cycle-within-pkg/conjure/com/palantir/errors/errors.conjure.go b/cycles/testdata/cycle-within-pkg/conjure/com/palantir/errors/errors.conjure.go index 0ac0bacd5..6606e9847 100644 --- a/cycles/testdata/cycle-within-pkg/conjure/com/palantir/errors/errors.conjure.go +++ b/cycles/testdata/cycle-within-pkg/conjure/com/palantir/errors/errors.conjure.go @@ -79,11 +79,8 @@ type MyError struct { // IsMyError returns true if err is an instance of MyError. func IsMyError(err error) bool { - if err == nil { - return false - } - _, ok := errors.GetConjureError(err).(*MyError) - return ok + cErr := errors.GetConjureError(err) + return cErr != nil && cErr.Name() == "Namespace:MyError" } func (e *MyError) Error() string { diff --git a/cycles/testdata/no-cycles/conjure/com/palantir/errors/errors.conjure.go b/cycles/testdata/no-cycles/conjure/com/palantir/errors/errors.conjure.go index c5940927d..736043bd9 100644 --- a/cycles/testdata/no-cycles/conjure/com/palantir/errors/errors.conjure.go +++ b/cycles/testdata/no-cycles/conjure/com/palantir/errors/errors.conjure.go @@ -79,11 +79,8 @@ type MyError struct { // IsMyError returns true if err is an instance of MyError. func IsMyError(err error) bool { - if err == nil { - return false - } - _, ok := errors.GetConjureError(err).(*MyError) - return ok + cErr := errors.GetConjureError(err) + return cErr != nil && cErr.Name() == "Namespace:MyError" } func (e *MyError) Error() string { diff --git a/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/errors/errors.conjure.go b/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/errors/errors.conjure.go index 11564fc78..141803b48 100644 --- a/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/errors/errors.conjure.go +++ b/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/errors/errors.conjure.go @@ -79,11 +79,8 @@ type MyError struct { // IsMyError returns true if err is an instance of MyError. func IsMyError(err error) bool { - if err == nil { - return false - } - _, ok := errors.GetConjureError(err).(*MyError) - return ok + cErr := errors.GetConjureError(err) + return cErr != nil && cErr.Name() == "Namespace:MyError" } func (e *MyError) Error() string { diff --git a/cycles/testdata/pkg-cycle/conjure/com/palantir/errors/errors.conjure.go b/cycles/testdata/pkg-cycle/conjure/com/palantir/errors/errors.conjure.go index 38c8f50ce..3561aa1c8 100644 --- a/cycles/testdata/pkg-cycle/conjure/com/palantir/errors/errors.conjure.go +++ b/cycles/testdata/pkg-cycle/conjure/com/palantir/errors/errors.conjure.go @@ -79,11 +79,8 @@ type MyError struct { // IsMyError returns true if err is an instance of MyError. func IsMyError(err error) bool { - if err == nil { - return false - } - _, ok := errors.GetConjureError(err).(*MyError) - return ok + cErr := errors.GetConjureError(err) + return cErr != nil && cErr.Name() == "Namespace:MyError" } func (e *MyError) Error() string { diff --git a/cycles/testdata/type-cycle/conjure/com/palantir/errors/errors.conjure.go b/cycles/testdata/type-cycle/conjure/com/palantir/errors/errors.conjure.go index 8f5521a99..b61776ce6 100644 --- a/cycles/testdata/type-cycle/conjure/com/palantir/errors/errors.conjure.go +++ b/cycles/testdata/type-cycle/conjure/com/palantir/errors/errors.conjure.go @@ -80,11 +80,8 @@ type MyError struct { // IsMyError returns true if err is an instance of MyError. func IsMyError(err error) bool { - if err == nil { - return false - } - _, ok := errors.GetConjureError(err).(*MyError) - return ok + cErr := errors.GetConjureError(err) + return cErr != nil && cErr.Name() == "Namespace:MyError" } func (e *MyError) Error() string { diff --git a/integration_test/testgenerated/errors/api/errors.conjure.go b/integration_test/testgenerated/errors/api/errors.conjure.go index 8098d9cb5..1771e8333 100644 --- a/integration_test/testgenerated/errors/api/errors.conjure.go +++ b/integration_test/testgenerated/errors/api/errors.conjure.go @@ -85,11 +85,8 @@ type MyInternal struct { // IsMyInternal returns true if err is an instance of MyInternal. func IsMyInternal(err error) bool { - if err == nil { - return false - } - _, ok := errors.GetConjureError(err).(*MyInternal) - return ok + cErr := errors.GetConjureError(err) + return cErr != nil && cErr.Name() == "MyNamespace:MyInternal" } func (e *MyInternal) Error() string { @@ -263,11 +260,8 @@ type MyNotFound struct { // IsMyNotFound returns true if err is an instance of MyNotFound. func IsMyNotFound(err error) bool { - if err == nil { - return false - } - _, ok := errors.GetConjureError(err).(*MyNotFound) - return ok + cErr := errors.GetConjureError(err) + return cErr != nil && cErr.Name() == "MyNamespace:MyNotFound" } func (e *MyNotFound) Error() string {