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

🐛 Fix: Prevent nil errors in log.Error to ensure proper logging and add custom linter to avoid this scenario in the future #1599

Merged

Conversation

camilamacedo86
Copy link
Contributor

@camilamacedo86 camilamacedo86 commented Jan 10, 2025

Description

Solves the following issues and add a custom linter to avoid this scenario in the future:

Furthermore, it solves a similar scenario in the EventHandler code implementation found with the new custom linter check.

Example os usage:

$ make lint
go build -tags containers_image_openpgp -o ./bin/custom-linter ./hack/ci/custom-linters/cmd
go vet -tags=containers_image_openpgp -vettool=./bin/custom-linter ./...
# github.com/operator-framework/operator-controller/cmd/operator-controller
# [github.com/operator-framework/operator-controller/cmd/operator-controller]
cmd/operator-controller/main.go:337:3: Incorrect usage of 'logger.Error(nil, ...)'. The first argument must be a non-nil 'error'. Passing 'nil' results in silent failures, making debugging harder.

🐛 **What is wrong?**
   setupLog.Error(nil, "unable to register finalizer", "finalizerKey", controllers.ClusterExtensionCleanupUnpackCacheFinalizer)

💡 **How to solve? Return the error, i.e.:**
   logger.Error(errors.New("kind error (i.e. configuration error)"), "unable to register finalizer", "key", value)


make: *** [lint-custom] Error 1

Reviewer Checklist

  • API Go Documentation
  • Tests: Unit Tests (and E2E Tests, if appropriate)
  • Comprehensive Commit Messages
  • Links to related GitHub Issue(s)

@camilamacedo86 camilamacedo86 requested a review from a team as a code owner January 10, 2025 20:45
Copy link

netlify bot commented Jan 10, 2025

Deploy Preview for olmv1 ready!

Name Link
🔨 Latest commit 457edd9
🔍 Latest deploy log https://app.netlify.com/sites/olmv1/deploys/67ab534624dc1f00082725c0
😎 Deploy Preview https://deploy-preview-1599--olmv1.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@camilamacedo86 camilamacedo86 changed the title 🐛 Fix error message 🐛 Fix: Prevent nil errors in setupLog.Error to ensure proper logging and add sanity check Jan 10, 2025
Copy link

codecov bot commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 43.44262% with 69 lines in your changes missing coverage. Please review.

Project coverage is 67.85%. Comparing base (9864ba7) to head (457edd9).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...nal/contentmanager/source/internal/eventhandler.go 3.33% 27 Missing and 2 partials ⚠️
.../custom-linters/analyzers/setuplognilerrorcheck.go 66.66% 19 Missing and 7 partials ⚠️
catalogd/cmd/catalogd/main.go 0.00% 6 Missing ⚠️
cmd/operator-controller/main.go 0.00% 6 Missing ⚠️
hack/ci/custom-linters/cmd/main.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1599      +/-   ##
==========================================
- Coverage   68.26%   67.85%   -0.42%     
==========================================
  Files          58       60       +2     
  Lines        4995     5096     +101     
==========================================
+ Hits         3410     3458      +48     
- Misses       1343     1388      +45     
- Partials      242      250       +8     
Flag Coverage Δ
e2e 52.35% <2.77%> (-0.39%) ⬇️
unit 55.43% <42.62%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@camilamacedo86 camilamacedo86 force-pushed the fix-error-message branch 2 times, most recently from 11b2c46 to a66fb03 Compare January 12, 2025 12:09
@camilamacedo86 camilamacedo86 changed the title 🐛 Fix: Prevent nil errors in setupLog.Error to ensure proper logging and add sanity check (WIP) 🐛 Fix: Prevent nil errors in setupLog.Error to ensure proper logging and add sanity check Jan 12, 2025
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 12, 2025
Makefile Outdated
Comment on lines 101 to 103
.PHONY: lint-custom
lint-custom: custom-linter-build
go vet -vettool=./bin/custom-linter ./...
Copy link
Member

Choose a reason for hiding this comment

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

golangci-lint should already be running go vet (assuming the govet linter is enabled). So I think we get this part automatically if we add the directory where we build the linter binary to the path for the golangci-lint run command.

Copy link
Contributor Author

@camilamacedo86 camilamacedo86 Feb 5, 2025

Choose a reason for hiding this comment

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

See https://golangci-lint.run/contributing/new-linters/
But I could not make it work.
I would need to spend more time on that.

Could we move forward with this one as it is and then track a task for we see if we can integrate it with golangci as a follow up?

At least now we have the logs fixed + the linter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created : #1775

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 25, 2025
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 4, 2025
@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 5, 2025
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 5, 2025
@camilamacedo86 camilamacedo86 force-pushed the fix-error-message branch 4 times, most recently from c4b2d95 to 2ba109f Compare February 5, 2025 13:49
@camilamacedo86 camilamacedo86 changed the title (WIP) 🐛 Fix: Prevent nil errors in setupLog.Error to ensure proper logging and add sanity check 🐛 Fix: Prevent nil errors in setupLog.Error to ensure proper logging and add sanity check Feb 5, 2025
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 5, 2025
@grokspawn
Copy link
Contributor

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Feb 10, 2025
@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Feb 10, 2025
@camilamacedo86
Copy link
Contributor Author

@tmshort rebased :-)

Copy link
Contributor

@tmshort tmshort Feb 10, 2025

Choose a reason for hiding this comment

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

There should also be an even number (2, 4, 6, etc) of arguments to log.Error():

  • Arg 1: error type
  • Arg 2: message
  • Arg 3+4: key1+value1
  • Arg 5+6: key2+value2

etc.
Is it possible to check that?

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 for all that does not fail to build already. See that:

Arg 2: message -> will fail to build
Arg 1: error type -> will fail to build / accept only nil or error

@camilamacedo86 camilamacedo86 force-pushed the fix-error-message branch 3 times, most recently from 9c6c0ac to 6cfe172 Compare February 11, 2025 13:14
….) calls and fix found linter issues.

Fix: Prevent nil errors in setupLog.Error to ensure proper logging

Closes; operator-framework#1566
Closes: operator-framework#1556

Furthermore, it solves a similar scenario in the EventHandler code implementation found with the new custom linter check.
Copy link
Contributor

@tmshort tmshort left a comment

Choose a reason for hiding this comment

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

/lgtm

@camilamacedo86 camilamacedo86 added this pull request to the merge queue Feb 11, 2025
@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Feb 11, 2025
Merged via the queue into operator-framework:main with commit 4304961 Feb 11, 2025
21 of 23 checks passed
@camilamacedo86 camilamacedo86 deleted the fix-error-message branch February 11, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix issues with logging flag validations
6 participants