Skip to content

feat(api): use golang error wrap for store error detection #13486

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Icarus9913
Copy link
Contributor

@Icarus9913 Icarus9913 commented Apr 29, 2025

Motivation

Before this PR, we had some error assertion by matching the error message string like:

func IsResourceNotFound(err error) bool {
	return err != nil && strings.HasPrefix(err.Error(), "resource not found")
}

func IsResourceAlreadyExists(err error) bool {
	return err != nil && strings.HasPrefix(err.Error(), "resource already exists")

So, I plan to use golang wrap to do the secondary encapsulation for error and supplement the error assertion.

Implementation information

  1. Define the specific brief error with ErrIsAlreadyExists = errors.New("already exists")
  2. Use fmt.Errorf("%w") to implement the secondary encapsulation for the need.
  3. Use errors.Is() to do the assertion with the basic error in above step1.

Supporting documentation

Fix #2604

Copy link
Contributor

Reviewer Checklist

🔍 Each of these sections need to be checked by the reviewer of the PR 🔍:
If something doesn't apply please check the box and add a justification if the reason is non obvious.

  • Is the PR title satisfactory? Is this part of a larger feature and should be grouped using > Changelog?
  • PR description is clear and complete. It Links to relevant issue as well as docs and UI issues
  • This will not break child repos: it doesn't hardcode values (.e.g "kumahq" as an image registry)
  • IPv6 is taken into account (.e.g: no string concatenation of host port)
  • Tests (Unit test, E2E tests, manual test on universal and k8s)
    • Don't forget ci/ labels to run additional/fewer tests
  • Does this contain a change that needs to be notified to users? In this case, UPGRADE.md should be updated.
  • Does it need to be backported according to the backporting policy? (this GH action will add "backport" label based on these file globs, if you want to prevent it from adding the "backport" label use no-backport-autolabel label)

Signed-off-by: Icarus Wu <[email protected]>
@Icarus9913 Icarus9913 changed the title feat(api): use golang error wrap for error detection feat(api): use golang error wrap for store error detection Apr 30, 2025
@Icarus9913 Icarus9913 marked this pull request as ready for review April 30, 2025 06:56
@Icarus9913 Icarus9913 requested a review from a team as a code owner April 30, 2025 06:56
@Icarus9913 Icarus9913 added the ci/run-full-matrix PR: Runs all possible e2e test combination (expensive use carefully) label Apr 30, 2025
@Icarus9913
Copy link
Contributor Author

I've checked the https://github.com/kumahq/kuma/tree/2.10.1/pkg/core/rest/errors
I think we can integrate them if needed in the future, then we won't define the errors seperately.

Signed-off-by: Icarus Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci/run-full-matrix PR: Runs all possible e2e test combination (expensive use carefully)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Go error type wrapping for store error detection
1 participant