carlos / platform Public

billingstripe: keep the decode reason when a listed object is refused

Final review finding I(a) (Fable P3-2): listAll mapped every refusal from
its decoder to a bare decode_error, so a held reconcile logged nothing
about which movement was refused or why. Error gains a Cause (with
Unwrap, printed as cause=...); listAll keeps the decoder's error there.
MoneyMovements names the balance movement and its type in the refusal,
and ChargePayments names a refund that belongs to another charge.
TestMoneyMovementsRefusalNamesTheMovement covers it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 294a1ea207e714297b99d908ec73d3d43c354552 parent 2983992
3 files changed, +70 −30
  • internal/billingstripe/client.go +13 −3
  • internal/billingstripe/money.go +37 −27
  • internal/billingstripe/money_test.go +20 −0
diff --git a/internal/billingstripe/client.go b/internal/billingstripe/client.go
index 02074b15d..55521e8d5 100644
--- a/internal/billingstripe/client.go
+++ b/internal/billingstripe/client.go
@@ -76,8 +76,14 @@ type Error struct {
// earlier request under the same idempotency key (its
// Idempotent-Replayed header), and ran nothing now.
Replayed bool
+ // Cause is why a CodeDecodeError was refused, when the decoder said:
+ // a list object that did not decode, naming what it was.
+ Cause error
}
+// Unwrap is the decode refusal's cause, nil when there is none.
+func (e *Error) Unwrap() error { return e.Cause }
+
// RefusedParam reports whether Stripe refused the request outright as
// invalid because of the named parameter. Such a request was not executed.
func (e *Error) RefusedParam(name string) bool {
@@ -95,6 +101,9 @@ func (e *Error) Error() string {
if e.StripeCode != "" {
parts = append(parts, "stripe_code="+e.StripeCode)
}
+ if e.Cause != nil {
+ parts = append(parts, "cause="+e.Cause.Error())
+ }
return strings.Join(parts, " ")
}
@@ -1058,8 +1067,9 @@ type listPage struct {
// errListDone to stop with success once what it looks for is found. A
// list longer than billing.MaxListPages pages is billing.ErrIncomplete; a
// page that says there is more but holds nothing, or repeats an id, is
-// broken pagination. Either way nothing is returned for the caller to act
-// on.
+// broken pagination. An object each refuses is a CodeDecodeError carrying
+// each's error as its Cause. Either way nothing is returned for the caller
+// to act on.
func (c *Client) listAll(ctx context.Context, path string, query url.Values, each func(raw json.RawMessage) (string, error)) error {
seen := map[string]bool{}
after := ""
@@ -1082,7 +1092,7 @@ func (c *Client) listAll(ctx context.Context, path string, query url.Values, eac
return nil
}
if err != nil {
- return &Error{Code: CodeDecodeError, HTTPStatus: http.StatusOK}
+ return &Error{Code: CodeDecodeError, HTTPStatus: http.StatusOK, Cause: err}
}
if !validID(id) || seen[id] {
return &Error{Code: CodeBrokenPagination}
diff --git a/internal/billingstripe/money.go b/internal/billingstripe/money.go
index 385eebd39..6302ec4a7 100644
--- a/internal/billingstripe/money.go
+++ b/internal/billingstripe/money.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
+ "fmt"
"net/http"
"net/url"
"slices"
@@ -232,32 +233,9 @@ func (p *Provider) MoneyMovements(ctx context.Context, from, to time.Time) ([]bi
if err := json.Unmarshal(raw, &t); err != nil {
return "", err
}
- m := billing.Movement{
- ID: t.ID, Kind: movementKindOf(t.Type), Type: t.Type, Source: t.Source.ID,
- Amount: t.Amount, Fee: t.Fee, Currency: t.Currency, At: t.Created,
- }
- // A charge, refund or refund failure must name its charge: the
- // money pass reads the charge to record and attribute it.
- switch m.Kind {
- case billing.MovementCharge:
- if !validID(t.Source.ID) {
- return "", errors.New("billingstripe: charge movement names no charge")
- }
- m.Charge = t.Source.ID
- case billing.MovementRefund, billing.MovementRefundFailure:
- if t.Source.Object == nil || !validID(t.Source.ID) {
- return "", errors.New("billingstripe: refund movement source not expanded")
- }
- var r struct {
- Charge idOrExpanded `json:"charge"`
- }
- if err := json.Unmarshal(t.Source.Object, &r); err != nil {
- return "", err
- }
- if !validID(r.Charge.ID) {
- return "", errors.New("billingstripe: refund movement names no charge")
- }
- m.Charge = r.Charge.ID
+ m, err := movementOf(t)
+ if err != nil {
+ return "", fmt.Errorf("balance movement %s (%s): %w", t.ID, t.Type, err)
}
out = append(out, m)
return t.ID, nil
@@ -275,6 +253,38 @@ func (p *Provider) MoneyMovements(ctx context.Context, from, to time.Time) ([]bi
return out, nil
}
+// movementOf maps one balance transaction onto the neutral Movement.
+func movementOf(t balanceTxn) (billing.Movement, error) {
+ m := billing.Movement{
+ ID: t.ID, Kind: movementKindOf(t.Type), Type: t.Type, Source: t.Source.ID,
+ Amount: t.Amount, Fee: t.Fee, Currency: t.Currency, At: t.Created,
+ }
+ // A charge, refund or refund failure must name its charge: the
+ // money pass reads the charge to record and attribute it.
+ switch m.Kind {
+ case billing.MovementCharge:
+ if !validID(t.Source.ID) {
+ return billing.Movement{}, errors.New("billingstripe: charge movement names no charge")
+ }
+ m.Charge = t.Source.ID
+ case billing.MovementRefund, billing.MovementRefundFailure:
+ if t.Source.Object == nil || !validID(t.Source.ID) {
+ return billing.Movement{}, errors.New("billingstripe: refund movement source not expanded")
+ }
+ var r struct {
+ Charge idOrExpanded `json:"charge"`
+ }
+ if err := json.Unmarshal(t.Source.Object, &r); err != nil {
+ return billing.Movement{}, err
+ }
+ if !validID(r.Charge.ID) {
+ return billing.Movement{}, errors.New("billingstripe: refund movement names no charge")
+ }
+ m.Charge = r.Charge.ID
+ }
+ return m, nil
+}
+
// chargeKindOf is what a charge paid for, from its invoice's billing
// reason. Only a subscription's first and renewal invoices are its
// payments; a change's proration and anything else is once-off.
@@ -329,7 +339,7 @@ func (p *Provider) ChargePayments(ctx context.Context, charge string) (billing.C
return "", err
}
if r.Charge != charge {
- return "", errors.New("billingstripe: a refund of another charge")
+ return "", fmt.Errorf("billingstripe: refund %s is of another charge", r.ID)
}
refunds = append(refunds, r)
return r.ID, nil
diff --git a/internal/billingstripe/money_test.go b/internal/billingstripe/money_test.go
index fd57d8d8b..54fa10785 100644
--- a/internal/billingstripe/money_test.go
+++ b/internal/billingstripe/money_test.go
@@ -194,6 +194,26 @@ func TestMoneyMovementsRefusesMalformed(t *testing.T) {
}
}
+// A refused movement reaches the log with its id and the reason, not a
+// bare decode_error: the error keeps the decoder's cause.
+func TestMoneyMovementsRefusalNamesTheMovement(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprintf(w, `{"data":[%s],"has_more":false}`,
+ txnJSON("txn_9", "refund", -1, 0, "eur", moneyT0, `{"id":"re_1","object":"refund"}`))
+ }))
+ defer srv.Close()
+ _, err := prepProvider(t, srv.URL, nil).MoneyMovements(context.Background(), moneyT0, moneyT0.Add(time.Hour))
+ var se *Error
+ if !errors.As(err, &se) || se.Code != CodeDecodeError || se.Cause == nil {
+ t.Fatalf("err = %#v, want a decode error with its cause", err)
+ }
+ for _, want := range []string{"txn_9", "refund movement names no charge"} {
+ if !strings.Contains(err.Error(), want) {
+ t.Errorf("error %q does not say %q", err, want)
+ }
+ }
+}
+
// chargeServer serves one charge and, optionally, its refunds list.
type chargeServer struct {
t *testing.T