Skip to content

Commit 521d6cd

Browse files
committed
fix(streaming): handle more AbortError cases
1 parent 569e55e commit 521d6cd

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

packages/bedrock-sdk/src/streaming.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class Stream<Item> extends CoreStream<Item> {
9090
done = true;
9191
} catch (e) {
9292
// If the user calls `stream.controller.abort()`, we should exit without throwing.
93-
if (e instanceof Error && e.name === 'AbortError') return;
93+
if (isAbortError(e)) return;
9494
throw e;
9595
} finally {
9696
// If the user `break`s, abort the ongoing request.
@@ -101,3 +101,14 @@ export class Stream<Item> extends CoreStream<Item> {
101101
return new Stream(iterator, controller);
102102
}
103103
}
104+
105+
function isAbortError(err: unknown) {
106+
return (
107+
typeof err === 'object' &&
108+
err !== null &&
109+
// Spec-compliant fetch implementations
110+
(('name' in err && (err as any).name === 'AbortError') ||
111+
// Expo fetch
112+
('message' in err && String((err as any).message).includes('FetchRequestCanceledException')))
113+
);
114+
}

src/lib/MessageStream.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isAbortError } from '../internal/errors';
12
import { AnthropicError, APIUserAbortError } from '../error';
23
import {
34
type ContentBlock,
@@ -289,7 +290,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
289290

290291
#handleError = (error: unknown) => {
291292
this.#errored = true;
292-
if (error instanceof Error && error.name === 'AbortError') {
293+
if (isAbortError(error)) {
293294
error = new APIUserAbortError();
294295
}
295296
if (error instanceof APIUserAbortError) {

0 commit comments

Comments
 (0)