Skip to content

Commit

Permalink
Properly handle event source errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Jan 17, 2025
1 parent e3b2857 commit a71f1c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"dependencies": {
"event-iterator": "^2.0.0",
"eventsource-parser": "^1.1.1"
"eventsource-parser": "^3.0.0"
},
"devDependencies": {
"swagger-typescript-api": "^13.0.3",
Expand Down
18 changes: 13 additions & 5 deletions packages/api/templates/http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const { apiConfig, generateResponses, config } = it;
%>
import { EventIterator } from 'event-iterator';
import { createParser, type ParsedEvent, type ReconnectInterval } from 'eventsource-parser';
import { createParser, type EventSourceMessage } from 'eventsource-parser';

export type QueryParamsType = Record<string | number, any>;
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
Expand Down Expand Up @@ -277,15 +277,23 @@ export class HttpClient<SecurityDataType = unknown> {
queue.stop();
}

const parser = createParser((event: ParsedEvent | ReconnectInterval) => {
if (event.type === 'event') {
if (event.data === 'done') {
const parser = createParser({
onEvent: (event: EventSourceMessage) => {
if (event.event === 'done') {
stop();
} else if (event.event === 'error') {
const data = JSON.parse(event.data);
reader.cancel();
queue.fail(new Error(data.error.message));
} else {
const data = JSON.parse(event.data);
queue.push(data);
}
}
},
onError: (error: Error) => {
reader.cancel();
queue.fail(error);
},
})

const decoder = new TextDecoder();
Expand Down

0 comments on commit a71f1c1

Please sign in to comment.