Skip to content

Propagate more errors#267

Open
fwiesel wants to merge 1 commit intomainfrom
more-no-swallow-fixes
Open

Propagate more errors#267
fwiesel wants to merge 1 commit intomainfrom
more-no-swallow-fixes

Conversation

@fwiesel
Copy link
Contributor

@fwiesel fwiesel commented Mar 13, 2026

Both in the traits controller as well as in the placement-api calls, some errors were swallowed.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error reporting so multiple errors are preserved and surfaced together.
    • Ensured cleanup operations propagate deletion errors instead of discarding them.
  • Tests

    • Added tests covering error handling when trait retrieval fails.
    • Added tests covering failures during consumer allocation deletion.

@fwiesel fwiesel requested a review from Copilot March 13, 2026 15:04
@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

📝 Walkthrough

Walkthrough

Update: propagate and preserve existing errors during status patching in TraitsController and return errors from consumer-allocation deletion in CleanupResourceProvider; tests added to assert both failure paths.

Changes

Cohort / File(s) Summary
TraitsController
internal/controller/traits_controller.go, internal/controller/traits_controller_test.go
When a status patch fails, the controller now joins the original reconcile error with the patch error (errors.Join(err, ...)). Added test context that simulates GetTraits returning 500 and asserts an error is returned and no PUT occurs.
Placement / Cleanup
internal/openstack/placement.go, internal/openstack/placement_test.go
CleanupResourceProvider now captures and returns errors from deleteConsumerAllocations instead of discarding them. Added a test context that mocks consumer-allocation DELETE returning 500 and asserts the cleanup surfaces an error.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • mchristianl

Poem

🐰 I nudge the logs at dawn,

Two errors held, not one withdrawn.
No tiny fault slips unseen,
Status tells what both have been.
Hops of joy — the code is keen!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Propagate more errors' directly summarizes the main objective of the PR: fixing error handling to propagate errors instead of swallowing them across multiple components (traits controller and placement-api).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch more-no-swallow-fixes
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes two cases where real errors were being dropped during cleanup/reconcile flows, ensuring callers get the underlying failure instead of a nil error.

Changes:

  • Propagate consumer allocation deletion errors in CleanupResourceProvider instead of discarding them.
  • Preserve the original GetTraits error when also patching status conditions in TraitsController.Reconcile.
  • Add regression tests covering both previously-swallowed error paths.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
internal/openstack/placement_test.go Adds a regression test ensuring allocation-delete failures are returned.
internal/openstack/placement.go Returns early when consumer allocation deletion fails (no longer swallows error).
internal/controller/traits_controller_test.go Adds a regression test ensuring GetTraits errors are not lost after status patching.
internal/controller/traits_controller.go Joins the original error with the status patch error instead of overwriting it.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
internal/openstack/placement_test.go (1)

323-327: Strengthen this test by asserting provider deletion is never reached.

Using a failing handler here would lock in the early-return behavior on consumer deletion errors.

🧪 Proposed test hardening
-				// The provider delete succeeds, so that it cannot accidentally surface
-				// the error for us — only the consumer allocation delete error should be returned.
-				fakeServer.Mux.HandleFunc(deleteProviderURL, func(w http.ResponseWriter, r *http.Request) {
-					w.WriteHeader(http.StatusNoContent)
-				})
+				// Provider delete should not be called when consumer allocation deletion fails.
+				fakeServer.Mux.HandleFunc(deleteProviderURL, func(w http.ResponseWriter, r *http.Request) {
+					defer GinkgoRecover()
+					Fail("should not be called")
+				})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/openstack/placement_test.go` around lines 323 - 327, The
provider-delete handler in the test currently returns 204 which lets the
provider path succeed and doesn't assert it was never called; change the
fakeServer.Mux.HandleFunc for deleteProviderURL so that if that handler is
invoked it fails the test (e.g., call t.Fatalf or t.Error with a clear message)
to assert provider deletion is never reached, and ensure the closure captures
the test *t* properly so the failure is reported.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/controller/traits_controller_test.go`:
- Around line 297-299: The comment describing a past bug (which says "err is
reassigned to errors.Join(patchErr) instead of errors.Join(originalErr,
patchErr)...") is now outdated and misleading; remove or replace that comment in
the test so it no longer documents pre-fix behavior — locate the comment
referencing errors.Join, patchErr, and GetTraits in the tests (in
traits_controller_test.go) and delete the bug-note or update it to a short,
accurate explanation of the current behavior.

In `@internal/openstack/placement_test.go`:
- Around line 337-339: Remove the outdated bug comment that mentions
deleteConsumerAllocations' return value being discarded in
internal/openstack/placement_test.go; the test and code have been updated so
that error handling is correct, so simply delete the three-line comment
referencing the bug to avoid misleading future readers and keep comments in sync
with the current behavior of deleteConsumerAllocations.

---

Nitpick comments:
In `@internal/openstack/placement_test.go`:
- Around line 323-327: The provider-delete handler in the test currently returns
204 which lets the provider path succeed and doesn't assert it was never called;
change the fakeServer.Mux.HandleFunc for deleteProviderURL so that if that
handler is invoked it fails the test (e.g., call t.Fatalf or t.Error with a
clear message) to assert provider deletion is never reached, and ensure the
closure captures the test *t* properly so the failure is reported.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f1006a62-4195-4f20-b6bc-8b60a85a42a9

📥 Commits

Reviewing files that changed from the base of the PR and between 05f22f6 and 0368bf3.

📒 Files selected for processing (4)
  • internal/controller/traits_controller.go
  • internal/controller/traits_controller_test.go
  • internal/openstack/placement.go
  • internal/openstack/placement_test.go

@fwiesel fwiesel force-pushed the more-no-swallow-fixes branch from 0368bf3 to 7a206a3 Compare March 13, 2026 15:19
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
internal/openstack/placement.go (1)

154-156: Include consumerID in propagated delete errors for diagnosability.

At Line 155, returning the raw error makes it harder to identify which consumer failed when multiple allocations are processed. Consider wrapping with context.

Suggested patch
-		if err := deleteConsumerAllocations(ctx, client, consumerID).Err; err != nil {
-			return err
+		if err := deleteConsumerAllocations(ctx, client, consumerID).Err; err != nil {
+			return fmt.Errorf("failed to delete allocations for consumer %s: %w", consumerID, err)
 		}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/openstack/placement.go` around lines 154 - 156, The delete error
returned from deleteConsumerAllocations is missing the consumer identifier;
update the error return in the block that calls deleteConsumerAllocations(ctx,
client, consumerID) so it wraps or annotates the underlying error with the
consumerID (e.g., use fmt.Errorf or errors.Wrapf) to produce a message like
"deleteConsumerAllocations failed for consumer %s: %w" referencing the
consumerID variable and the original err; modify the return at the call site
(the code that currently returns err) to return the wrapped/annotated error
instead.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@internal/openstack/placement.go`:
- Around line 154-156: The delete error returned from deleteConsumerAllocations
is missing the consumer identifier; update the error return in the block that
calls deleteConsumerAllocations(ctx, client, consumerID) so it wraps or
annotates the underlying error with the consumerID (e.g., use fmt.Errorf or
errors.Wrapf) to produce a message like "deleteConsumerAllocations failed for
consumer %s: %w" referencing the consumerID variable and the original err;
modify the return at the call site (the code that currently returns err) to
return the wrapped/annotated error instead.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fe8990c4-61ed-413e-b93d-fe02b2389bf0

📥 Commits

Reviewing files that changed from the base of the PR and between 0368bf3 and 7a206a3.

📒 Files selected for processing (4)
  • internal/controller/traits_controller.go
  • internal/controller/traits_controller_test.go
  • internal/openstack/placement.go
  • internal/openstack/placement_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/openstack/placement_test.go

Both in the traits controller as well as in the placement-api
calls, some errors were swallowed.
@fwiesel fwiesel force-pushed the more-no-swallow-fixes branch from 7a206a3 to 0a4b33b Compare March 13, 2026 15:28
@github-actions
Copy link

Merging this branch will increase overall coverage

Impacted Packages Coverage Δ 🤖
github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller 63.95% (+0.86%) 👍
github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack 79.10% (+1.66%) 👍

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller/traits_controller.go 73.44% (+12.50%) 64 47 (+8) 17 (-8) 🎉
github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack/placement.go 93.48% (+4.59%) 46 (+1) 43 (+3) 3 (-2) 👍

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller/traits_controller_test.go
  • github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack/placement_test.go

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