Skip to content

Commit fb42e58

Browse files
Jayko001claude
andcommitted
add not_found variant so callers don't need a try/catch
Adds `{ status: 'not_found' }` to AcquireOutcome and wraps NotFoundError internally so all three documented outcomes — acquired, timed_out, not_found — are reachable via the same `result.status` switch. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 7d72251 commit fb42e58

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

src/lib/browser-pools.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { NotFoundError } from '../core/error';
12
import type { Kernel } from '../client';
23
import type { RequestOptions } from '../internal/request-options';
34
import type { BrowserPoolAcquireParams, BrowserPoolAcquireResponse } from '../resources/browser-pools';
45

56
export type AcquireOutcome =
67
| { status: 'acquired'; browser: BrowserPoolAcquireResponse }
7-
| { status: 'timed_out' };
8+
| { status: 'timed_out' }
9+
| { status: 'not_found' };
810

911
/**
1012
* Long-polling acquire that surfaces the HTTP outcome as a typed result.
@@ -14,18 +16,26 @@ export type AcquireOutcome =
1416
* - `{ status: 'acquired', browser }` — a browser was leased from the pool.
1517
* - `{ status: 'timed_out' }` — the long poll expired without a browser becoming
1618
* available. Retry to keep waiting.
19+
* - `{ status: 'not_found' }` — no pool exists with the given id or name.
1720
*
18-
* Rejects with `NotFoundError` if the pool does not exist.
21+
* Other API errors (auth, server errors, etc.) still reject.
1922
*/
2023
export async function acquire(
2124
client: Kernel,
2225
idOrName: string,
2326
body: BrowserPoolAcquireParams = {},
2427
options?: RequestOptions,
2528
): Promise<AcquireOutcome> {
26-
const { data, response } = await client.browserPools.acquire(idOrName, body, options).withResponse();
27-
if (response.status === 204) {
28-
return { status: 'timed_out' };
29+
try {
30+
const { data, response } = await client.browserPools.acquire(idOrName, body, options).withResponse();
31+
if (response.status === 204) {
32+
return { status: 'timed_out' };
33+
}
34+
return { status: 'acquired', browser: data };
35+
} catch (err) {
36+
if (err instanceof NotFoundError) {
37+
return { status: 'not_found' };
38+
}
39+
throw err;
2940
}
30-
return { status: 'acquired', browser: data };
3141
}

tests/lib/browser-pools.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Kernel, { NotFoundError } from '@onkernel/sdk';
1+
import Kernel from '@onkernel/sdk';
22

33
import { acquire, type AcquireOutcome } from '../../src/lib/browser-pools';
44

@@ -32,10 +32,11 @@ describe('browser pool typed acquire', () => {
3232
expect(result.status).toBe('timed_out');
3333
});
3434

35-
test('rejects with NotFoundError on 404', async () => {
35+
test('resolves to not_found on 404', async () => {
3636
const client = clientWith(async () =>
3737
Response.json({ code: 'not_found', message: 'pool not found' }, { status: 404 }),
3838
);
39-
await expect(acquire(client, 'missing')).rejects.toBeInstanceOf(NotFoundError);
39+
const result = await acquire(client, 'missing');
40+
expect(result.status).toBe('not_found');
4041
});
4142
});

0 commit comments

Comments
 (0)