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

refactor(cwg): replace deprecated errors module #12725

Merged
merged 2 commits into from Jun 15, 2022

Conversation

MoritzThomasHuebner
Copy link
Contributor

@MoritzThomasHuebner MoritzThomasHuebner commented May 16, 2022

Fixes #12632 for description

Summary

Remove dependency on "github.com/pkg/errors"

  • Replace use of errors.Wrap[f] with fmt.Errorf
  • Replace use of pkg errors.New with standard lib errors.New
  • Update go.mod files
  • Replace use of errors.New(fmt.Sprintf(...)) with fmt.Errorf
  • The error handling in addToStore in ue_store_utils.go and in getUE in uesim.go was incorrect.
    • In these database transactions commitErr and rollbackErr were not logged.
    • We restructured the switch/case logic to if/else
    • In the else bracket (err != nil), we now use a multierror to propagate both err and rollbackErr. The logic here is that the rollback should only be triggered if an error happened before, so both errors should be logged.

Test Plan

Run unit tests

Additional Information

Worked in pairing with @wolfseb

  • This change is backwards-breaking

@MoritzThomasHuebner MoritzThomasHuebner requested review from a team and uri200 May 16, 2022 06:39
@pull-request-size pull-request-size bot added the size/L Denotes a Pull Request that changes 100-499 lines. label May 16, 2022
@github-actions
Copy link
Contributor

Thanks for opening a PR! 💯

A couple initial guidelines

Howto

  • Reviews. The "Reviewers" listed for this PR are the Magma maintainers who will shepherd it.
  • Checks. All required CI checks must pass before merge.
  • Merge. Once approved and passing CI checks, use the ready2merge label to indicate the maintainers can merge your PR.

More info

Please take a moment to read through the Magma project's

If this is your first Magma PR, also consider reading

@MoritzThomasHuebner MoritzThomasHuebner marked this pull request as draft May 16, 2022 06:47
@MoritzThomasHuebner MoritzThomasHuebner changed the title refactor(cwf): Replaces errors.Wrap[f] with fmt.Errorf. refactor(cwg): Replaces errors.Wrap[f] with fmt.Errorf. May 16, 2022
@github-actions
Copy link
Contributor

github-actions bot commented May 16, 2022

cloud-workflow

979 tests   979 ✔️  2m 32s ⏱️
355 suites      0 💤
    7 files        0

Results for commit b10f7a9.

♻️ This comment has been updated with latest results.

@MoritzThomasHuebner MoritzThomasHuebner force-pushed the Remove_pkg/err_from_cwf branch 2 times, most recently from 5fc09d4 to 9d9e837 Compare May 16, 2022 09:01
@MoritzThomasHuebner MoritzThomasHuebner marked this pull request as ready for review May 16, 2022 23:59
@MoritzThomasHuebner MoritzThomasHuebner changed the title refactor(cwg): Replaces errors.Wrap[f] with fmt.Errorf. refactor(cwg): replace deprecated errors module May 17, 2022
@@ -45,7 +44,7 @@ func (m *GatewayCwfConfigs) ValidateModel(context.Context) error {
for _, peer := range m.AllowedGrePeers {
for _, key := range set[peer.IP] {
if swag.Uint32Value(peer.Key) == key {
return errors.New(fmt.Sprintf("Found duplicate peer %s with key %d", peer.IP, key))
return fmt.Errorf("Found duplicate peer %s with key %d", peer.IP, key)
Copy link
Contributor

Choose a reason for hiding this comment

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

Noticed that the PR description says this would be substituted with errors.New from std lib. I think this way might be nicer (you don't need the Sprintf). But may be adapt the PR summary?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. Also, I stumbled across a few cases like this in #12724, where there was a Sprintf inside an errors.New. When we just change to errors.New from the std lib, the linter will complain and suggest using fmt.Errorf instead, as it is done here.
But yes, we should add this case to the summary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the summary.

Comment on lines +42 to +43
errs := multierror.Append(err, fmt.Errorf("Error while rolling back transaction: %w", rollbackErr))
err = ConvertStorageErrorToGrpcStatus(errs.ErrorOrNil())
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to use multierror in one case but not in the other? And for using multierror at all?

And why do we start passing commitErr and rollbackErr?

There is a similar case in cwf/gateway/services/uesim/servicers/uesim.go.

Copy link
Contributor

@wolfseb wolfseb May 18, 2022

Choose a reason for hiding this comment

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

If err is nil, but the commit fails, there is only one error to be passed on. If err is not nil, then it tries to roll back, but if that also fails, we have two errors to pass: err and rollbackErr, that's why we use a multierror here.

We need to pass commitErr , because err is nil by default here due to the if clause, otherwise we would find an error, but the error we pass is nil, so in the end the error gets ignored.
In the other case we use the multierror to pass on both, err and rollbackErr.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with @wolfseb

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, so this goes beyond replacing pkg/errors and fixes broken error handling. Probably should mention that in the summary as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this to the summary.

@MoritzThomasHuebner MoritzThomasHuebner added ready2merge This PR is ready to be merged (is approved and passes all required checks) and removed ready2merge This PR is ready to be merged (is approved and passes all required checks) labels May 18, 2022
@MoritzThomasHuebner MoritzThomasHuebner force-pushed the Remove_pkg/err_from_cwf branch 3 times, most recently from 8cecfd4 to 85592dc Compare May 24, 2022 02:25
@magma magma deleted a comment from github-actions bot May 24, 2022
@MoritzThomasHuebner MoritzThomasHuebner force-pushed the Remove_pkg/err_from_cwf branch 4 times, most recently from 1ff0966 to d0e2cb2 Compare May 31, 2022 04:59
@alexzurbonsen
Copy link
Contributor

@uri200 Want to bring this PR to your attention. Would be great to get a review. Thanks!

@MoritzThomasHuebner MoritzThomasHuebner added the ready2merge This PR is ready to be merged (is approved and passes all required checks) label Jun 15, 2022
Signed-off-by: Moritz Huebner <moritz.huebner@tngtech.com>
Also fixes an incorrect use of commit/rollback error handling in ue_store_utils.go and uesim.go.

Signed-off-by: Moritz Huebner <moritz.huebner@tngtech.com>
@sebathomas sebathomas merged commit 6992af1 into magma:master Jun 15, 2022
@MoritzThomasHuebner MoritzThomasHuebner deleted the Remove_pkg/err_from_cwf branch July 18, 2022 07:47
emakeev pushed a commit to emakeev/magma that referenced this pull request Aug 5, 2022
* refactor(cwf): Replaces errors.Wrap[f] with fmt.Errorf.

Signed-off-by: Moritz Huebner <moritz.huebner@tngtech.com>

* refactor(cwf): Replaces more pkg/errors usages with std lib.

Also fixes an incorrect use of commit/rollback error handling in ue_store_utils.go and uesim.go.

Signed-off-by: Moritz Huebner <moritz.huebner@tngtech.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: cwf ready2merge This PR is ready to be merged (is approved and passes all required checks) size/L Denotes a Pull Request that changes 100-499 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check whether github.com/pkg/errors can be replaced with standard library functions
7 participants