Skip to content

fix(charges): preserve trigger and charge context in unhandled invoice patch errors - #5458

Merged
borosr merged 2 commits into
mainfrom
fix/charges-unhandled-invoice-patches-context
Nov 9, 2026
Merged

borosr merged 2 commits into
mainfrom
fix/charges-unhandled-invoice-patches-context

Conversation

@borosr

@borosr borosr commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • 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.

RetriggerView in GreptileConfidence 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.

Reviews (5) · Last reviewed commit: "test(billing): inline one-use invoice re..." · Reviewed by Greptile

@borosr borosr self-assigned this Nov 6, 2026
@borosr
borosr requested a review from turip November 6, 2026 14:09
@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Changes: Billing validation context

Layer / File(s) Summary
Structured unhandled-patch errors
openmeter/billing/charges/statemachine/machine.go, openmeter/billing/charges/statemachine/machine_test.go
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.
Validation context conversion
openmeter/billing/validationissue_test.go
Tests cover formatted wrapper text, typed issue data, billing component context, and context supplied through ValidationWithMessage.
Invoice hook validation outcomes
test/billing/lineengine_validationcontext_test.go
A sandbox-hook test checks invoice approval behavior for wrapped plain errors and typed issues, including persisted validation issue data.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • 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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

Commits

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.

@borosr
borosr force-pushed the fix/charges-unhandled-invoice-patches-context branch from d9defb3 to 8f18dbc Compare November 6, 2026 14:10
turip
turip previously approved these changes Nov 6, 2026

@turip turip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks good.


// A plain error from a line engine's issued hook is a system error, not a validation issue, so
// the whole approval attempt rolls back: no failed status and no issue row.
func (s *LineEngineTestSuite) TestOnInvoiceIssuedSystemErrorRollsBackWithoutPersistedIssues() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we already have some test for this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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.

@borosr borosr added kind/bug Something isn't working release-note/bug-fix Release note: Bug Fixes labels Nov 9, 2026
@borosr
borosr force-pushed the fix/charges-unhandled-invoice-patches-context branch from 8f18dbc to c28b18a Compare November 9, 2026 07:10
@borosr
borosr marked this pull request as ready for review November 9, 2026 07:10
@borosr
borosr requested a review from a team as a code owner November 9, 2026 07:10

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional comments: 1

test/billing/lineengine_validationcontext_test.go: 1

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.

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f255963 and c28b18a.

Files selected for processing: 4

  • openmeter/billing/charges/statemachine/machine.go
  • openmeter/billing/charges/statemachine/machine_test.go
  • openmeter/billing/validationissue_test.go
  • 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; 7 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional comments: 1

test/billing/lineengine_validationcontext_test.go: 1

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.

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f255963 and 754eea0.

Files selected for processing: 4

  • openmeter/billing/charges/statemachine/machine.go
  • openmeter/billing/charges/statemachine/machine_test.go
  • openmeter/billing/validationissue_test.go
  • 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; 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.
@borosr
borosr force-pushed the fix/charges-unhandled-invoice-patches-context branch from 754eea0 to 5bf661f Compare November 9, 2026 10:31
@borosr
borosr merged commit eb177e5 into main Nov 9, 2026
32 checks passed
@borosr
borosr deleted the fix/charges-unhandled-invoice-patches-context branch November 9, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants