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

feat: Print detailed entity information when it already exists #204

Merged
merged 3 commits into from
Sep 2, 2020

Conversation

cwurm
Copy link
Contributor

@cwurm cwurm commented Aug 20, 2020

Currently, when an entity is duplicated in the state file(s) the error message is not very helpful, e.g.:

$ deck validate
Error: entity already exists

In a large and distributed configuration, it is often not easy to figure out which entity this is. Also, the error message itself does not say where the entity already exists - it could mean exists in Kong.

With this PR, the error message becomes a lot more helpful:

$ ./deck validate
Error: error reading state file(s): consumer consumer1 already exists

This applies across deck sync, deck diff, and deck validate.

@cwurm cwurm requested a review from hbagdi August 20, 2020 13:30
Copy link
Member

@hbagdi hbagdi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition, it requires some minor polish.

Again, relatively a small diff but the gains will be manifold!

state/utils.go Outdated
@@ -13,9 +13,6 @@ const (
// returned when an entity is not found in the state.
var ErrNotFound = errors.New("entity not found")

// ErrAlreadyExists represents an entity is already present in the state.
var ErrAlreadyExists = errors.New("entity already exists")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this back and use this error. We want this error to be part of the public API of this package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -46,7 +47,7 @@ func (k *UpstreamsCollection) Add(upstream Upstream) error {
}
_, err := getUpstream(txn, searchBy...)
if err == nil {
return ErrAlreadyExists
return errors.Errorf("upstream %v already exists", upstream.Console())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please instead change this to:

return fmt.Errorf("inserting upstream %v: %w", upstream.Console())

This blog post and other related material on error propagation in Go might be helpful here as to why we want to do this. tldr; we want to make it easy for users of the package to know the type of error occurs here so that the the user can fail gracefully (we don't do use this error currently but we can in future).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same comment applies for other entities as well.

Meta: PRs like this always show that we need to remove this duplication by using code-generation or some such tool. We will eventually get to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to keep ErrAlreadyExists would that rather be:

return errors.Wrapf(ErrAlreadyExists, "inserting upstream %v", upstream.Console())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That and %w is essentially the same. Please read the blog post I've linked in my previous comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I get it now. Thanks for the link!

file/reader.go Outdated
return builder.build()
state, err := builder.build()
if err != nil {
return nil, errors.Wrapf(err, "error reading state file(s)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please instead change the wrap comment to "building state".
We would like to reserve read verb for Read() like methods or the read syscall.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@cwurm
Copy link
Contributor Author

cwurm commented Sep 2, 2020

@hbagdi Addressed the comments, let me know what you think.

The error message now reads:

$ ./deck validate -s consumers1.yaml -s consumers2.yaml
Error: error building state: inserting consumer kong: entity already exists

@cwurm cwurm requested a review from hbagdi September 2, 2020 13:06
file/reader.go Outdated
return builder.build()
state, err := builder.build()
if err != nil {
return nil, errors.Wrap(err, "error building state")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the wrap string to building state?
As you can see in the output, error is printed twice, which is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@hbagdi hbagdi merged commit ca6efd0 into Kong:main Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants