carlos / platform Public

billingstripe: a 5xx with a truncated body stays indeterminate (plans 6b branch review)

classify keeps a received 5xx indeterminate when its body read fails
(CodeNetwork with the status set), instead of ErrNoResponse, which the
console would resend (fact 5, Invariant 8).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 08992fcb0ddbb297e4c2f9bd94bed396df9cdfe4 parent 387f139
2 files changed, +45 −2
  • internal/billingstripe/prepared.go +8 −2
  • internal/billingstripe/prepared_test.go +37 −0
diff --git a/internal/billingstripe/prepared.go b/internal/billingstripe/prepared.go
index 5c7135844..5ecce0d52 100644
--- a/internal/billingstripe/prepared.go
+++ b/internal/billingstripe/prepared.go
@@ -238,8 +238,9 @@ func decodeResult(prep billing.Prepared, raw json.RawMessage, date time.Time, re
// classes);
// - 429: ErrNotExecuted;
// - any other 4xx: ErrRefused;
-// - 5xx, a redirect, or an answer that cannot be read: ErrIndeterminate;
-// - a transport error or timeout: ErrNoResponse;
+// - 5xx (its body read or not), a redirect, or an answer that cannot be
+// read: ErrIndeterminate;
+// - a transport error or timeout before any status: ErrNoResponse;
// - a request refused before it was sent: ErrRefused.
//
// The *Error stays wrapped for the log: its text carries no response body
@@ -253,6 +254,11 @@ func classify(err error) error {
switch e.Code {
case CodeTimeout, CodeNetwork:
class = billing.ErrNoResponse
+ if e.HTTPStatus >= 500 {
+ // The status arrived and the body was lost: still a 5xx, so still
+ // indeterminate (fact 5), never a resend (Invariant 8).
+ class = billing.ErrIndeterminate
+ }
case CodeMissingIdempotencyKey, CodeInvalidParams, CodeInvalidID:
class = billing.ErrRefused
case CodeAPIError:
diff --git a/internal/billingstripe/prepared_test.go b/internal/billingstripe/prepared_test.go
index 560d401e6..4270b6c1a 100644
--- a/internal/billingstripe/prepared_test.go
+++ b/internal/billingstripe/prepared_test.go
@@ -445,6 +445,43 @@ func TestOutcomeClasses(t *testing.T) {
}
}
+// TestTruncated5xxStaysIndeterminate: a 5xx whose body is cut off mid-read
+// is still a 5xx. Its outcome is indeterminate (fact 5, Invariant 8), never
+// ErrNoResponse, which a caller would resend.
+func TestTruncated5xxStaysIndeterminate(t *testing.T) {
+ for _, status := range []int{500, 502, 503} {
+ t.Run(fmt.Sprint(status), func(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Length", "200")
+ w.WriteHeader(status)
+ fmt.Fprint(w, `{"error":{"type":"api_e`)
+ w.(http.Flusher).Flush()
+ panic(http.ErrAbortHandler) // close the connection mid-body
+ }))
+ defer srv.Close()
+ p := prepProvider(t, srv.URL, nil)
+ for name, prep := range allPrepared(t, p) {
+ _, err := p.Send(context.Background(), prep)
+ if !errors.Is(err, billing.ErrIndeterminate) || errors.Is(err, billing.ErrNoResponse) {
+ t.Errorf("%s: truncated %d = %v, want ErrIndeterminate only", name, status, err)
+ }
+ }
+ })
+ }
+ // A transport failure before any status stays ErrNoResponse.
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ hj, _ := w.(http.Hijacker)
+ conn, _, _ := hj.Hijack()
+ conn.Close()
+ }))
+ defer srv.Close()
+ p := prepProvider(t, srv.URL, nil)
+ prep, _ := p.PrepareRenewal("sub_1", true, "key-1")
+ if _, err := p.Send(context.Background(), prep); !errors.Is(err, billing.ErrNoResponse) {
+ t.Fatalf("no answer = %v, want ErrNoResponse", err)
+ }
+}
+
func TestReplayedAndProviderTime(t *testing.T) {
status := http.StatusOK
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {