Skip to content

Commit f03d3cb

Browse files
authored
Merge pull request #262 from francescov1/fix-stream-error-handling
Fix stream error handling
2 parents a7fcc26 + 4a79315 commit f03d3cb

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/streaming.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,26 @@ export class Stream<Item> implements AsyncIterable<Item> {
8888
}
8989

9090
if (sse.event === 'error') {
91-
throw APIError;
91+
let errorData: any;
92+
try {
93+
errorData = JSON.parse(sse.data);
94+
} catch (e) {
95+
console.error('Could not parse message into JSON:', sse.data);
96+
console.error('From chunk:', sse.raw);
97+
throw new APIError(
98+
undefined,
99+
undefined,
100+
'Unknown error',
101+
undefined
102+
);
103+
}
104+
105+
throw new APIError(
106+
undefined,
107+
errorData,
108+
errorData.message,
109+
undefined
110+
);
92111
}
93112
}
94113
} catch (e) {

0 commit comments

Comments
 (0)