Add error handling to streaming

This commit is contained in:
William Oldham
2023-12-19 17:43:36 +00:00
parent 6377a9ccaa
commit 207cdbb220

View File

@@ -88,6 +88,7 @@ app.get('/scrape', async (context) => {
} }
return streamSSE(context, async (stream) => { return streamSSE(context, async (stream) => {
try {
const output = await providers.runAll({ const output = await providers.runAll({
media, media,
events: { events: {
@@ -107,10 +108,20 @@ app.get('/scrape', async (context) => {
}); });
if (output) { if (output) {
return await writeSSEEvent(stream, 'completed', output); await writeSSEEvent(stream, 'completed', output);
return await stream.close();
} }
return await writeSSEEvent(stream, 'noOutput', ''); await writeSSEEvent(stream, 'noOutput', '');
return await stream.close();
} catch (e: any) {
await writeSSEEvent(stream, 'error', {
name: e.name,
message: e.message,
stack: e.stack,
});
return await stream.close();
}
}); });
}); });
@@ -145,6 +156,7 @@ app.get('/scrape/embed', async (context) => {
} }
return streamSSE(context, async (stream) => { return streamSSE(context, async (stream) => {
try {
const output = await providers.runEmbedScraper({ const output = await providers.runEmbedScraper({
id: embedInput.id, id: embedInput.id,
url: embedInput.url, url: embedInput.url,
@@ -156,14 +168,24 @@ app.get('/scrape/embed', async (context) => {
}); });
if (output) { if (output) {
return await writeSSEEvent(stream, 'completed', output); await writeSSEEvent(stream, 'completed', output);
return await stream.close();
} }
return await writeSSEEvent(stream, 'noOutput', ''); await writeSSEEvent(stream, 'noOutput', '');
return await stream.close();
} catch (e: any) {
await writeSSEEvent(stream, 'error', {
name: e.name,
message: e.message,
stack: e.stack,
});
return await stream.close();
}
}); });
}); });
app.get('/scrape/embed', async (context) => { app.get('/scrape/source', async (context) => {
const queryParams = context.req.query(); const queryParams = context.req.query();
const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED); const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED);
@@ -194,6 +216,7 @@ app.get('/scrape/embed', async (context) => {
} }
return streamSSE(context, async (stream) => { return streamSSE(context, async (stream) => {
try {
const output = await providers.runSourceScraper({ const output = await providers.runSourceScraper({
id: sourceInput.id, id: sourceInput.id,
media: sourceInput, media: sourceInput,
@@ -205,10 +228,20 @@ app.get('/scrape/embed', async (context) => {
}); });
if (output) { if (output) {
return await writeSSEEvent(stream, 'completed', output); await writeSSEEvent(stream, 'completed', output);
return await stream.close();
} }
return await writeSSEEvent(stream, 'noOutput', ''); await writeSSEEvent(stream, 'noOutput', '');
return await stream.close();
} catch (e: any) {
await writeSSEEvent(stream, 'error', {
name: e.name,
message: e.message,
stack: e.stack,
});
return await stream.close();
}
}); });
}); });