Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/transport-http-requeue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@smooai/observability': patch
---

TypeScript: re-queue ingest batches when native `fetch` resolves a non-2xx response.

Native `fetch` resolves normally for HTTP 4xx and 5xx responses. The transport only handled rejected promises, so a non-2xx ingest response removed the batch from the queue permanently instead of retrying it. The transport now treats `response.ok === false` as a failure and pushes the batch back onto the queue for the existing retry path.
10 changes: 10 additions & 0 deletions packages/core/src/__tests__/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ describe('Transport', () => {
// the queue after the throw rather than being dropped.
expect(t._queueSize()).toBe(1);
});

it('re-queues the batch when native fetch resolves a non-2xx response', async () => {
const fetcher = vi.fn().mockResolvedValue({ ok: false, status: 503 });
const t = new Transport({ dsn: 'https://example.com', maxBatchSize: 1, flushIntervalMs: 1000 }, { canBeacon: false, fetcher });
t.enqueue(evt('a'));
await vi.runOnlyPendingTimersAsync();
await Promise.resolve();
expect(fetcher).toHaveBeenCalled();
expect(t._queueSize()).toBe(1);
});
});
3 changes: 2 additions & 1 deletion packages/core/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ export class Transport {
// (`@smooai/fetch`) — it owns retries/timeouts/circuit-breaking.
// Fall back to global fetch when no fetcher was injected (tests).
const fetcher = this.adapter.fetcher ?? ((url, init) => fetch(url, init));
await fetcher(this.opts.dsn, {
const response = await fetcher(this.opts.dsn, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(payload),
keepalive: true,
});
if (response.ok === false) throw new Error('ingest request failed');
} catch {
// Best-effort: push events back to the front of the queue for next attempt.
this.queue.unshift(...batch);
Expand Down