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

improvement: Generated errors IsError functions match by name, not go type assertion #643

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-643.v2.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 6 additions & 8 deletions conjure/errorwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
)
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions integration_test/testgenerated/errors/api/errors.conjure.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.