fix(usage): malformed tenant id lists no events on postgres
pgx reports an invalid UUID from rows.Err, not from Query, so GET /v1/usage/events?tenant_id=not-a-uuid answered 500 on Postgres and 200 on the memory store. It now returns an empty list on both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
524f5009d3
commit
37032e649e
2 files changed
+22
-1
No files matched your search
@@ -154,3 +154,16 @@ func TestUsageSummary_emptyAndBadInput(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// The event list answers a malformed tenant id like an unknown one: an empty
|
||||
// list. Postgres reports the bad UUID only once the rows are read.
|
||||
func TestUsageEvents_malformedTenantIsEmpty(t *testing.T) {
|
||||
eachStore(t, func(t *testing.T, h *testHarness) {
|
||||
for _, id := range []string{"not-a-uuid", "00000000-0000-0000-0000-0000000000ff"} {
|
||||
resp, raw := h.do("GET", "/v1/usage/events?tenant_id="+id, nil)
|
||||
if resp.StatusCode != 200 || !bytes.Contains(raw, []byte(`[]`)) {
|
||||
t.Fatalf("events %s: %d %s", id, resp.StatusCode, raw)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -173,7 +173,15 @@ func (p *Postgres) ListUsageEvents(ctx context.Context, f UsageFilter) ([]UsageE
|
||||
}
|
||||
out = append(out, *u)
|
||||
}
|
||||
return out, rows.Err()
|
||||
// pgx reports a malformed tenant id only once the rows are read, not
|
||||
// from Query: a tenant id that is not a UUID matches no events.
|
||||
if err := rows.Err(); err != nil {
|
||||
if isInvalidUUID(err) {
|
||||
return []UsageEvent{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) MarkUsageSynced(ctx context.Context, ids []int64, at time.Time) error {
|
||||
|
||||
Reference in new issue
Block a user