You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(charges): preserve trigger and charge context in unhandled invoice patch errors - #5458
The unhandled-invoice-patches rejections in the charge state machine wrapped the sentinel with fmt.Errorf. Once billing converted them into a persisted validation issue only the bare leaf text survived, so the trigger and charge were lost. This is the same class of issue #5449 closed for unsupported operations.
ErrUnhandledInvoicePatches rejections now return a structured models.Error with trigger and charge_id attributes, keeping the sentinel as the cause so errors.Is matching is unchanged.
Converter unit test showing how fmt.Errorf wrapper context is dropped by ToValidationIssues and which wrappers do carry context.
State machine test reproducing the lost trigger and charge context (fails without the fix).
LineEngineTestSuite test characterizing the payment-settled boundary through the sandbox post-advance hook: a plain line engine error rolls the trigger back without persisting anything, while a typed payment error is persisted with its attributes under the app component.
Observations surfaced by the tests, not addressed here
The persisted component of an identical line engine failure depends on which path drove the trigger: the sandbox post-advance hook yields the app component because componentWrapper keeps the outermost component, while the direct trigger path yields the engine component.
WithHTTPStatusCodeAttribute leaks into persisted invoice validation issues under the key commonhttp.httpAttributeKey:openmeter.http.status_code because Attributes.AsStringMap renders typed keys as %T:%v.
Test plan
go test ./openmeter/billing/charges/...
go test ./openmeter/billing/ -run TestToValidationIssuesDropsFormattedWrapperContext
POSTGRES_HOST=127.0.0.1 go test ./test/billing/ -run TestLineEngine
make lint-go-fast
Summary by CodeRabbit
Bug Fixes
Billing errors for unhandled invoice changes now include relevant charge and trigger details.
Validation messages retain useful error codes, attributes, and underlying error details without unrelated wrapper text.
Invoice approval now handles typed validation issues consistently: typed issues are recorded, while plain errors do not create validation issues.
Payment-processing validation issues show the underlying error and relevant context, making failures easier to understand.
Confidence Score: 5/5
The PR appears safe to merge; the previous finding is fixed and no new actionable issues were found.
Summary
The PR preserves charge and trigger details when billing converts unhandled invoice patch errors into validation issues.
Charge errors for unhandled invoice patches keep their trigger and charge details.
No actionable comments were generated in the recent review.
Walkthrough
The state machine now returns structured errors when invoice patches remain unhandled. Tests cover validation context conversion and invoice approval outcomes for wrapped plain and typed hook errors.
The state machine reports unhandled invoice patches with an error code, cause, status, charge ID, and optional trigger. A test checks that the validation issue retains this context.
openmeterio/openmeter#5449: Updates the same state machine and applies a similar pattern to preserve trigger and charge context for unsupported-operation errors.
Suggested labels:area/billing
Suggested reviewers:galexihu
Merge Risk: Minimal · up to fadc6
The change adds structured context to unhandled-patch errors. No concrete merge-blocking regression was established.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the primary change: trigger and charge context is now preserved in unhandled invoice patch errors. The raw summary confirms the state machine change returns a structured error with tri…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the primary change: trigger and charge context is now preserved in unhandled invoice patch errors. The raw summary confirms the state machine change returns a structured error with trigger and charge ID attributes, and the added tests cover that behavior. The 'fix(charges):' prefix is a conventional-commit scope and does not reduce clarity. The title is 83 characters, which is slightly long but still readable.
Finishing Touches
Generate docstrings
Commit to this branch
Create a new PR
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between 5bf661f and fadc673.
Files selected for processing: 1
test/billing/lineengine_validationcontext_test.go
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Agreed, TestOnPaymentAuthorizedFailureTransitionsToRetryableAuthorizationState already covers a plain hook error rolling the trigger back with no persisted issues, and the issued hook goes through the same runLineEngineInvoiceSteps path. Dropped the test.
126-126: Trivial · Maintainability & Code Quality · Low value
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Heads-up: this assertion is brittle.
The expected key commonhttp.httpAttributeKey:openmeter.http.status_code comes from the %T:%v formatting in AsStringMap. It will break if that package or type is renamed. The PR notes call this out as a known quirk. A rename in commonhttp would break this test with no link to this feature.
Consider asserting only charge_id plus a presence check on the status code. That keeps the test focused on the validation-context behavior.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
124-127: Trivial · Maintainability & Code Quality · Low value
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Heads-up: the status-code attribute key is brittle.
The test hardcodes the typed-key string "commonhttp.httpAttributeKey:openmeter.http.status_code" and the value float64(400). The PR description already calls this out as a known quirk. If the key's type name or package path changes, the test fails for a reason unrelated to this change. It would be a bit sturdier to assert only charge_id and check the status-code entry separately. You can also leave it as is, since the quirk is documented.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
…e patch errors
The unhandled-invoice-patches rejections wrapped the sentinel with
fmt.Errorf, so once billing converted them into a persisted validation
issue only the bare leaf text survived and the trigger and charge were
lost, the same gap #5449 closed for unsupported operations.
Return a structured error carrying the trigger and charge_id as
attributes instead, keeping ErrUnhandledInvoicePatches as the cause so
errors.Is matching is unchanged.
Add tests characterizing how fmt.Errorf wrapper context is dropped by
the validation issue conversion, how a plain line engine error rolls the
trigger back without persisting anything, and how a typed payment error
reached through the sandbox post-advance hook is persisted with its
attributes under the app component.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The unhandled-invoice-patches rejections in the charge state machine wrapped the sentinel with
fmt.Errorf. Once billing converted them into a persisted validation issue only the bare leaf text survived, so the trigger and charge were lost. This is the same class of issue #5449 closed for unsupported operations.ErrUnhandledInvoicePatchesrejections now return a structuredmodels.Errorwithtriggerandcharge_idattributes, keeping the sentinel as the cause soerrors.Ismatching is unchanged.fmt.Errorfwrapper context is dropped byToValidationIssuesand which wrappers do carry context.LineEngineTestSuitetest characterizing the payment-settled boundary through the sandbox post-advance hook: a plain line engine error rolls the trigger back without persisting anything, while a typed payment error is persisted with its attributes under the app component.Observations surfaced by the tests, not addressed here
componentWrapperkeeps the outermost component, while the direct trigger path yields the engine component.WithHTTPStatusCodeAttributeleaks into persisted invoice validation issues under the keycommonhttp.httpAttributeKey:openmeter.http.status_codebecauseAttributes.AsStringMaprenders typed keys as%T:%v.Test plan
go test ./openmeter/billing/charges/...go test ./openmeter/billing/ -run TestToValidationIssuesDropsFormattedWrapperContextPOSTGRES_HOST=127.0.0.1 go test ./test/billing/ -run TestLineEnginemake lint-go-fastSummary by CodeRabbit


Confidence Score: 5/5
The PR appears safe to merge; the previous finding is fixed and no new actionable issues were found.Summary
The PR preserves charge and trigger details when billing converts unhandled invoice patch errors into validation issues.
Reviews (5) · Last reviewed commit: "test(billing): inline one-use invoice re..." · Reviewed by Greptile